How would you write the following two dimensional array in JSON schema? The grid is fixed to 16*13. It contains completely empty rows or row with values like int(0-99) or an empty string.
Here is an example of the array:
[
[],
[],
[],
[],
[],
[],
['','','','',94,78,37,78,'','','',61,71],
[42,82,53,62,65,47,65,77,26,93,69,69,51],
[38,07,47,06,87,90,21,41,50,24,55,45,24],
[55,69,'','','',83,04,90,34,88,99,28,71],
[11,08,91,62,'','','','',36,53,57,76,65],
[21,85,34,62,'','','','',76,67,20,77,85],
[72,73,34,26,'','','','',37,22,49,89,26],
[84,11,19,84,34,53,19,08,10,12,31,62,24],
[36,94,43,27,71,30,86,96,37,45,19,60,50],
[31,05,27,74,10,33,22,07,03,77,82,23,50]
]
I wonder what is the best way to write this without hundreds LOC...
Thanks in advance!
A JSON Schema for an array can be seen as set of restrictions that must apply on the elements of the array. In the next sections we specificate each restriction and when a JSON document validates against these restrictions. We use these restrictions to restrict the elements of the array.
There are two ways in which arrays are generally used in JSON: 1 List validation: a sequence of arbitrary length where each item matches the same schema. 2 Tuple validation: a sequence of fixed length where each item may have a different schema. In this usage, the index (or... More ...
Because JSON doesn't have multi-dimensional arrays, just arrays. Any given element in an array may be another array, but it's perfectly valid for only some of them to be. E.g., [ [1, 2, 3], "foo", {}] – T.J. Crowder Nov 5 '13 at 16:00 Say thanks for this answer.
On the other hand, the following JSON does validate against the schema above: We can restrict the contents of an array using a JSON Schema. For example, if we want to be sure that the items of the array are strings and that we could have at most 3 items we could use the following schema: The following JSON document would validate again the schema:
OK, so let's build this up by parts.
First, a single entry in the grid, either an empty string or an integer.
{
"oneOf": [
{
"enum": [""]
},
{
"type": "integer",
"minimum": 0,
"maximum": 99
}
]
}
Next, let's define a single row - this can be empty, or exactly 13 items long:
{
"type": "array",
"items": {"$ref": "#/definitions/gridCell"},
"oneOf": [
{"enum": [[]]}, // Alternatively: {"maxItems": 0}
{"minItems": 13, "maxItems": 13}
]
}
Now, we just want an array of 16 of these:
{
"type": "array",
"items": {"$ref": "#/definitions/gridRow"},
"minItems": 16,
"maxItems": 16,
"definitions": {
"gridCell": { ... schema from step #1 ... },
"gridRow": { ... schema from step #2 ... }
}
}
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