6

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.

NitinK
  • 103
  • 1
  • 7

3 Answers3

8

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.

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
  • Thanks Jruizaranguren.. I have tested it. I will try it in my solution if it works. But I think it will help me. Thanks a lot. – NitinK Dec 20 '15 at 18:09
  • Hi Jruizaranguren, your solution worked well. I got into another issue mentioned as below. REQUIRED is not working array elements. I have described problem as below. Thanks for your help in advance. – NitinK Jan 04 '16 at 11:06
  • Shouldn't this be a new question? BTW REQUIRED is a keyword that applies only to object elements. – jruizaranguren Jan 04 '16 at 11:15
  • added this question in the same thread just to correalate with old post only. I will add new question. Thanks for help. – NitinK Jan 05 '16 at 05:09
5

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"
  ]
}
Jens Dossé
  • 51
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 12 '21 at 14:39
0
  {
  "Table1": {
    "Data": [
      [
        100,
        "Test",
        2.5
      ],
      [
        101,
        "Test1",
        5.5
      ]
    ]
  }
}

Above is sample json & its schema is as below

    {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net",
  "type": "object",
  "properties": {
    "Table1": {
      "id": "http://jsonschema.net/Table1",
      "type": "object",
      "properties": {
        "Data": {
          "id": "http://jsonschema.net/Table1/Data",
          "type": "array",
          "items": {
            "type": "array",
            "items": [
              {
                "id": "http://jsonschema.net/Table1/Data/0/0",
                "type": "integer"
              },
              {
                "id": "http://jsonschema.net/Table1/Data/0/1",
                "type": "string"
              },
              {
                "id": "http://jsonschema.net/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.

NitinK
  • 103
  • 1
  • 7