Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json schema for a map of similar objects

I wish to write a json schema to cover this (simplified) example

{
    "errorMessage": "",
    "nbRunningQueries": 0,
    "isError": False,
    "result": {
        "foo": {"price":10.0, "country":"UK"},
        "bar": {"price":100.2, "country":"UK"}
    }
}

which can have this pretty trivial root schema

schema = {
    "type":"object",
    "required":True,
    "properties":{
        "errorMessage": {"type":"string", "required":True},
        "isError": {"type":"boolean", "required":True},
        "nbRunningQueries": {"type":"number", "required":True},
        "result": {"type":"object","required":True}
    }
}

The complication is the results {} element. Unlike a standard pattern where results would be an array of same objects - each with an id field or similar this response models a python dictionary which looks like this:

{
    "foo": {},
    "bar": {},
    ...
}

So given that a will be getting a results object of flexible size with no set keys how can I write json schema for this?

I don't control the input sadly or I'd rewrite it to be something like

{
    "errorMessage": "",
    "nbRunningQueries": 0,
    "isError": False,
    "result": [
        {"id": "foo", "price":10.0, "country": "UK"},
        {"id": "bar", "price":100.2, "country":"UK"}
    ]
}

Any help or links to pertinent examples would be great. Thanks.

like image 965
Transact Charlie Avatar asked Dec 08 '14 12:12

Transact Charlie


People also ask

How do I pass a JSON object to a map?

We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format. Initially, we can create a POJO class and pass this instance as an argument to the put() method of Map class and finally add this map instance to the accumulateAll() method of JSONObject.

What is JSON object schema?

JSON (JavaScript Object Notation) is a simple and lightweight text-based data format. JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it.

What is oneOf in JSON Schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.


1 Answers

With json-schema draft 4, you can use additionalProperties keyword to specify the schema of any new properties that you could receive in your results object.

"result" : {
    "type" : "object"
    "additionalProperties" : {
        "type" : "number"
    }
}

If you can restrict the allowed key names, then you may use "patternProperties" keyword and a regular expression to limit the permited key names.

Note that in json-schema draft 4 "required" must be an array which is bounded to the object, not to each property.

like image 61
jruizaranguren Avatar answered Oct 21 '22 10:10

jruizaranguren