Why am I getting an 'unexpected token' error with this Minecraft /give command?

I'm trying to make a sign that you can right click and it will clear cacti from your inventory, then give you 18 cookies named "$100 Cookie" with the lore "$100". But when I put it in a command block and power it, the command block says this:

[04:23:51] Data tag parsing failed: Unexpected token '$' at: $100 Cookie\",Lore:[$100]}}\"}}",Text3:"{\"text\":\"iIiIiIi\",\"color\":\"light_purple\",\"strikethrough\":true,\"obfuscated\":true}",Text4:"{\"text\":\"[Sell]\",\"color\":\"dark_purple\",\"bold\":true}"

The command I'm using is:

/give @p sign 1 0 {BlockEntityTag:{Text1:"{\"text\":\"[Sell]\",\"color\":\"dark_purple\",\"bold\":true,\"clickEvent\":{\"action\":\"run_command\",\"value\":\"clear @p[name=rainbowlack,r=3] cactus 0 2304\"}}",Text2:"{\"text\":\"2304 Cacti\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"give @p[name=rainbowlack,r=3] minecraft:cookie 18 0 {display:{Name:\"$100 Cookie\",Lore:[$100]}}\"}}",Text3:"{\"text\":\"iIiIiIi\",\"color\":\"light_purple\",\"strikethrough\":true,\"obfuscated\":true}",Text4:"{\"text\":\"[Sell]\",\"color\":\"dark_purple\",\"bold\":true}"},display:{Name:"Custom Sign"}}

What's wrong with this command? I've tried backslashes and stuff. Can you please try to fix the command and test out your potential fix in-game? Thank you!

You'll have to change the [name=rainbowlack,r=3] parts to whatever your username is (example: [name=playerplayer123,r=3]).


You need to add more escape characters to deeper-nested quotation marks. You open the Text1 tag with a ", thus the very next " character closes it. You open the value tag for the click event with \", meaning the next \" you use will close it, which occurs in your nested /give command's NBT data (specifically just after the Name tag). That it what's causing your issue.

The formula to determine the number of backslashes required is: 2n+1, where n is the number of current backslashes. For the nested NBT data, you need 3 backslashes per quotation mark (\\\").

Fixed command:

/give @p sign 1 0 {BlockEntityTag:{Text1:"{\"text\":\"[Sell]\",\"color\":\"dark_purple\",\"bold\":true,\"clickEvent\":{\"action\":\"run_command\",\"value\":\"clear @p[name=rainbowlack,r=3] cactus 0 2304\"}}",Text2:"{\"text\":\"2304 Cacti\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"give @p[name=rainbowlack,r=3] minecraft:cookie 18 0 {display:{Name:\\\"$100 Cookie\\\",Lore:[$100]}}\"}}",Text3:"{\"text\":\"iIiIiIi\",\"color\":\"light_purple\",\"strikethrough\":true,\"obfuscated\":true}",Text4:"{\"text\":\"[Sell]\",\"color\":\"dark_purple\",\"bold\":true}"},display:{Name:"Custom Sign"}}