I have a json object that can contain any number of nested objects with certain specification, for example:
{
"Bob": {
"age": "42",
"gender": "male"
},
"Alice": {
"age": "37",
"gender": "female"
}
}
And would like to have a schema looking something like:
{
"type": "object",
"propertySchema": {
"type": "object",
"required": [
"age",
"gender"
],
"properties": {
"age": {
"type": "string"
},
"gender": {
"type": "string"
}
}
}
}
I know that I can turn that into array and push 'name' inside the objects. In that case my schema would look like:
{
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"age",
"gender"
],
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "string"
},
"gender": {
"type": "string"
}
}
}
}
but I would like to have a dictionary-like structure. Is it possible to make such schema?
additionalProperties is your keyword:
{
"type" : "object",
"additionalProperties" : {
"type" : "object",
"required" : [
"age",
"gender"
],
"properties" : {
"age" : {
"type" : "string"
},
"gender" : {
"type" : "string"
}
}
}
}
additionalProperties
can have following values with different meanings:
"additionalProperties": false
No more properties are allowed at all."additionalProperties": true
Any more properties are allowed. This is the default behavior."additionalProperties": {"type": "string"}
Additional properties (of arbitrary name) are allowed, if they have value of given type ("string" here)."additionalProperties": {*any schema*}
Additional properties must satisfy the provided schema, such as the example provided above.