Nested objects with unknown keys - how to specify? #399
-
Hi all! I'm new to JSON Schema. Having a great time with the The situation is something like this: I have an object, which is a container for a list of objects which are all identical. Each object has its own name. So one way to do with would be something like: {
"devices" : [
{
"name" : "dev1",
....
},
{
"name" : "dev1",
....
},
....
]
} The above would be quite easy to model in JSON Schema. The important bit would be something like: {
"type" : "array",
"items" : {
"type" : "object"
"$ref" : "device.json"
}
} BUT ... this is not a very "canonical" JSON way to do things. What I'd rather do is {
"devices" : {
"dev1" : {
....
},
"dev2" : {
....
},
...
} Where the key names "dev1" and "dev2" are completely arbitrary, but the object is same in every case. I can't figure out how to model this, because it seems that all the tools for modelling an object require me to have some knowledge of the property (key) names. One idea I had was to use a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think you want to just specify {
"type": "object",
"additionalProperties": { ... }
} The meta-schemas actually do this for a number of properties, like |
Beta Was this translation helpful? Give feedback.
I think you want to just specify
additionalProperties
, which just takes a schema. Often thefalse
schema is used to indicate that there should be no properties other than the ones specified by the schema, but if you have arbitrary keys where the values must all be the same shape, then you can put the schema for the values in this.The meta-schemas actually do this for a number of properties, like
$defs
andproperties
where the keys can be anything, but the values need to be valid schemas.