[Question] Can't parse JSON. MalformedJsonException
Insane96 opened this issue ยท 4 comments
I have this string
jungleMain.addItemEntryHelper(<minecraft:bow>, 1, 0, [Functions.enchantWithLevels(1, 10, true), Functions.enchantRandomly(["infinity"]), Functions.enchantRandomly(["mending"]), Functions.parse("{\"function\": \"set_nbt\", \"tag\": \"{RepairCost:40,display:{Name:\\\"Bow of the Ancients\\\"}}\"}")], [Conditions.randomChance(0.8)]);
I get MalformedJsonException: Expected name at line 1 column 2 path $
I've even tried with
jungleMain.addItemEntryHelper(<minecraft:bow>, 1, 0, [Functions.enchantWithLevels(1, 10, true), Functions.enchantRandomly(["infinity"]), Functions.enchantRandomly(["mending"]), Functions.parse("{function: 'set_nbt',tag: '{RepairCost:40,display:{Name:\"Bow of the Ancients\"}}'}")], [Conditions.randomChance(0.8)]);
same problem. I can't figure out what I'm doing wrong.
You cannot use double quotes (") inside double quotes, even escaped. You must use single quotes (') instead of double quotes, then it will work. If you aren't already, use a text editor that has syntax highlighting. If the whole string doesn't have the right highlighting, it's unparseable.
Already tried something like this too
jungleMain.addItemEntryHelper(minecraft:bow, 1, 0, [Functions.enchantWithLevels(1, 10, true), Functions.enchantRandomly(["infinity"]), Functions.enchantRandomly(["mending"]), Functions.parse("{function: 'set_nbt',tag: '{RepairCost:40,display:{Name:\'Bow of the Ancients\'}}'}")], [Conditions.randomChance(0.8)]);
Without the " double quotes
OK, so I've discovered that it is impossible to properly interpret JSON from double quote delimited string, due to the fact that single quotes are invalid string delimiters in the JSON spec. However I also discovered that CraftTweaker can take raw NBT, so I've found a solution to your problem. You can do what you want like this. I plan to modify Functions.setNBT() to take raw NBT instead of a string, which will fix the NBT problem with it too.