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?
JSON at its top-level is a dictionary of attribute/value pairs, or key/value pairs as we've talked about dictionaries in this class. The values are numbers, strings, other dictionaries, and lists.
In a JSON file, Arrays are denoted by [ ] and dictionaries are denoted by { } .
JSON Schema is a JSON media type for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data.
JSON represents objects as name/value pairs, just like a Python dictionary. Serialization is the process of encoding data into JSON format (like converting a Python list to JSON). Deserialization is the process of decoding JSON data back into native objects you can work with (like reading JSON data into a Python list).
additionalProperties
is your keyword:
{ "type" : "object", "additionalProperties" : { "type" : "object", "required" : [ "age", "gender" ], "properties" : { "age" : { "type" : "string" }, "gender" : { "type" : "string" } } } }
additionalProperties
can have the following values with different meanings:
"additionalProperties": false
No more properties are allowed at all."additionalProperties": true
Other properties are allowed. This is the default behavior."additionalProperties": {"type": "string"}
Additional properties (of arbitrary name) are allowed, as long as their value follows the given type ("string"
here)."additionalProperties": {*any schema*}
Additional properties must satisfy the provided schema, such as the example provided above.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With