Procedurally create/read from nbt object/dictionary

Solution 1:

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"