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 as below
{
"$schema": "",
"id": "",
"type": "object",
"properties": {
"Data": {
"id": "",
"type": "array",
"items": [
{
"id": "",
"type": "array",
"items": [
{
"id": "",
"type": "integer"
},
{
"id": "",
"type": "string"
},
{
"id": "",
"type": "number"
}
],
"required": [
"0",
"1",
"2"
]
},
{
"id": "",
"type": "array",
"items": [
{
"id": "",
"type": "integer"
},
{
"id": "",
"type": "string"
},
{
"id": "",
"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.
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 as below
{
"$schema": "http://json-schema/draft-04/schema#",
"id": "http://jsonschema",
"type": "object",
"properties": {
"Data": {
"id": "http://jsonschema/Data",
"type": "array",
"items": [
{
"id": "http://jsonschema/Data/0",
"type": "array",
"items": [
{
"id": "http://jsonschema/Data/0/0",
"type": "integer"
},
{
"id": "http://jsonschema/Data/0/1",
"type": "string"
},
{
"id": "http://jsonschema/Data/0/2",
"type": "number"
}
],
"required": [
"0",
"1",
"2"
]
},
{
"id": "http://jsonschema/Data/1",
"type": "array",
"items": [
{
"id": "http://jsonschema/Data/1/0",
"type": "integer"
},
{
"id": "http://jsonschema/Data/1/1",
"type": "string"
},
{
"id": "http://jsonschema/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.
Share Improve this question edited Dec 17, 2015 at 13:35 NitinK asked Dec 17, 2015 at 12:15 NitinKNitinK 1231 gold badge1 silver badge7 bronze badges3 Answers
Reset to default 8If 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"
]
}
{
"Table1": {
"Data": [
[
100,
"Test",
2.5
],
[
101,
"Test1",
5.5
]
]
}
}
Above is sample json & its schema is as below
{
"$schema": "http://json-schema/draft-04/schema#",
"id": "http://jsonschema",
"type": "object",
"properties": {
"Table1": {
"id": "http://jsonschema/Table1",
"type": "object",
"properties": {
"Data": {
"id": "http://jsonschema/Table1/Data",
"type": "array",
"items": {
"type": "array",
"items": [
{
"id": "http://jsonschema/Table1/Data/0/0",
"type": "integer"
},
{
"id": "http://jsonschema/Table1/Data/0/1",
"type": "string"
},
{
"id": "http://jsonschema/Table1/Data/0/2",
"type": "number"
}
],
"additionalItems": false,
"required": [
"0",
"1",
"2"
]
}
}
},
"required": [
"Data"
]
}
}
}
This schema works for all rows of Data but its required property is not working somehow. Eventhough I am expecting all 3 columns data. It accepts the rows with 1 or 2 columns as well. If anybody has any idea. Please correct me.
[ 101 ], [ 101, "TEST3" ]
are also valid records of data which is not expected.