Reputation: 23
I need to know how to define a JSON schema from the following example which seems to be an array but the items are variable, the list of header "numbers" goes on and on. It's a sample web response from the web service.
{
"1453237200": {
"ERRORS_OUT": null,
"OCTETS_OUT": 229.727436984742,
"PACKETS_OUT": 1.87192892826381,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.83524341392888,
"OCTETS_IN": 234.715031103113,
"ERRORS_IN": null,
"IFSTATUS": 1
},
"1453240800": {
"ERRORS_OUT": null,
"OCTETS_OUT": 75.3713074074074,
"PACKETS_OUT": 0.418027777777778,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 0.418567592592593,
"OCTETS_IN": 78.1113824074074,
"ERRORS_IN": null,
"IFSTATUS": 1
},
"1453244400": {
"ERRORS_OUT": null,
"OCTETS_OUT": 75.2197541312908,
"PACKETS_OUT": 0.414839344161437,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 0.636818377014889,
"OCTETS_IN": 108.708849501661,
"ERRORS_IN": null,
"IFSTATUS": 1
}
}
Does anyone know how to handle this situation?
Upvotes: 0
Views: 2571
Reputation: 129797
This is not a JSON array, as it does not begin and end with [
and ]
. It is a JSON object, where the property names can vary. In code, you would represent this with a Dictionary
, Map
or associative array, depending on which language you were using.
To define a schema for this, it seems you'll need to use the patternProperties
construct with a regex to match the variable property names, as defined in section 5.4.4 of the JSON schema spec. Something like this:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"patternProperties":
{
"^[0-9]+$":
{
"type": "object",
"properties":
{
"OCTETS_OUT": { "type": "number" },
"PACKETS_OUT": { "type": "number" },
...
}
}
},
"additionalProperties": false
}
The JSON schema documentation shows an example of using patternProperties
here.
Upvotes: 3