Thanks in advance.
I am new to JSON & JSON schema. Tried to generate JSON schema for array of tuples. but it is not validating multiple records like a loop for all similar types of tuples. Below is json sample.
{
"Data":
[
[ 100, "Test", 2.5 ],
[ 101, "Test1", 3.5]
]
}
I have generated schema using site jsonschema.net as below
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net",
"type": "object",
"properties": {
"Data": {
"id": "http://jsonschema.net/Data",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Data/0",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Data/0/0",
"type": "integer"
},
{
"id": "http://jsonschema.net/Data/0/1",
"type": "string"
},
{
"id": "http://jsonschema.net/Data/0/2",
"type": "number"
}
],
"required": [
"0",
"1",
"2"
]
},
{
"id": "http://jsonschema.net/Data/1",
"type": "array",
"items": [
{
"id": "http://jsonschema.net/Data/1/0",
"type": "integer"
},
{
"id": "http://jsonschema.net/Data/1/1",
"type": "string"
},
{
"id": "http://jsonschema.net/Data/1/2",
"type": "number"
}
]
}
],
"required": [
"0",
"1"
]
}
},
"required": [
"Data"
]
}
If you see, it is creating schema for every tuple of similar type. Please help me to create a schema to validate each tuple in a generic way. Tuple count may vary.
If you want the inner array to have all items of the same kind you may use an object instead of an array. The following schema validates your example:
{
"type" : "object",
"properties" : {
"Data" : {
"type" : "array",
"items" : {
"type" : "array",
"items" : [{
"type" : "integer"
}, {
"type" : "string"
}, {
"type" : "number"
}
]
}
}
}
}
I have tested it here.
The JSON schema has a new syntax for tuples and the solution previously suggested by jruizaranguren can now be written more precisely in this way:
{
"type": "object",
"properties": {
"Data": {
"type": "array",
"items": [
{
"type": "array",
"prefixItems": [
{ "type": "integer" },
{ "type": "string" },
{ "type": "integer" }
],
"minItems": 3,
"items": false
}
]
}
},
"required": [
"Data"
]
}
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