I'm working on a project that requires creating nbt dictionaries from another nbt array.
Essentially, the scenerio is as follows
I have array [1,2,3,4] and I would like to create a dictionary {1:2, 3:4}
These are just example values and will be generated at runtime
However, I can't figure out a way to create a dictionary procedurally, as the only way I know is to do /data modify storage <name> <key> set value <value>
I know I can do set from instead, but the main issue is the hardcoded <key>. I can't figure out a way to procedurally create/remove a key from a dict.
1 Answer
Dynamic keys are not a thing in NBT.
You'll have to use this slightly more complex format for a key-value pair dictionary:
{ Database: [ {key: 1, value: "one"}, {key: 2, value: "two"}, {key: 3, value: "three"} ]
}…and read/write to it with these commands:
New K/V pair (static):
data modify storage test:main Database append value {key: 4, value: "four"}Read value from key (static):
tellraw @a {"nbt":"Database[{key: 4}].value"}Overwrite new value (dynamic) to a key (static):
data modify storage test:main Database[{key: 4}].value …
Commands to do the same operations but with dynamic keys will be much harder but not impossible. Here is a starter for adding a new key-value pair with dynamic key and value:
data modify storage test:main Database append value {key: "temp", value: "temp"}
data modify storage test:main Database[-1].key set value 5
data modify storage test:main Database[-1].value set value "five" 3