Why is my JSON text not displaying anything?
I understand that to display a raw score in raw JSON text, the score
component is used:
{"score":{"name":"@p"," objective":"test"}}
So I'm using the following command to set a zombie's custom name based on a score:
data modify entity @e[limit=1,type=zombie,sort=nearest] CustomName set value '{"score":{"name":"@e[limit=1,type=zombie,sort=nearest]","objective":"test"}}'
But unfortunately, this doesn't seem to display a score, even though I'm sure a score is instantiated.
This also seems to happen with these too:
{"selector":"@a"}
{"nbt":"MyPath","storage":"test:test"}
What can I do to make this work?
The answer to this lies within JSON component resolution. The JSON parser needs to resolve the data before it is displayed. For example, when you type:
[{"text":"You have "},{"score":{"name":"@p"," objective":"test"}},{"text":" point(s)."}]
It needs to convert the score
component into a text
component containing the proper number:
[{"text":"You have "},{"text":"5"},{"text":" point(s)."}]
This conversion (called resolution) can only happen from commands, signs, and loot table functions. In order to resolve JSON text automatically, we need to use a sign, which will always resolve JSON text immediately.
This means we will need a sign in our world. The standard location for this sign is (−30000000, 0, 1603). This standard location was chosen because it is outside the world border so no one can interact with it, plus a few other factors1:
setblock -30000000 0 1603 oak_wall_sign[facing=south]
Then a command to put the JSON text onto the sign would look like:
data modify block -30000000 0 1603 Text1 set value '{"score":{"name":"@e[limit=1,type=zombie,sort=nearest]","objective":"test"}}'
Then we write that data:
data modify entity @e[limit=1,type=zombie,sort=nearest] CustomName set from block -30000000 0 1603 Text1
1: Only chunk that is outside the WB, easy to remember, last two digits are 0, isn't 0