Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema for Array of tuples

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.

like image 803
NitinK Avatar asked Feb 09 '23 06:02

NitinK


2 Answers

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.

like image 102
jruizaranguren Avatar answered Feb 10 '23 23:02

jruizaranguren


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"
  ]
}
like image 40
Jens Dossé Avatar answered Feb 11 '23 00:02

Jens Dossé