Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary-like JSON schema

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?

like image 822
ironic Avatar asked Dec 08 '14 12:12

ironic


People also ask

Can JSON be a dictionary?

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.

What does a dictionary look like in JSON?

In a JSON file, Arrays are denoted by [ ] and dictionaries are denoted by { } .

What are definitions in JSON Schema?

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.

Is JSON a list or dictionary Python?

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).


1 Answers

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.
like image 152
jruizaranguren Avatar answered Oct 03 '22 20:10

jruizaranguren