"Data tag parsing failed" when trying to setblock a command block with a command in it [duplicate]

I'm trying to fix this but it doesn't work. Inside the command block. I typed in

/setblock 125 64 617 minecraft:command_block 0 replace {Command:"testfor @a {Inventory:[{id:"minecraft:bedrock"}]}"}

and it said:

[09:58:00] Data tag parsing failed: Unexpected token 'm' at: minecraft:bedrock"}]}"

enter image description here


The quotation mark at the end of id:" makes Minecraft think your Command tag ends right there. It is followed by m, which minecraft is confused about, since it expects either a comma, followed by a new tag, or a closing bracket.

To prevent this, you have to escape the "inner" quotation marks using a backslash (\) character, like so:

/setblock 125 64 617 minecraft:command_block 0 replace {Command:"testfor @a {Inventory:[{id:\"minecraft:bedrock\"}]}"}

\" is a literal quotation mark character, which the game doesn't confuse for the end of the string.

Note: Should you ever encounter even deeper nested strings (this can happen when you use raw JSON text with clickEvents or such), you have to escape the escape characters as well, e.g. \\\"