简体   繁体   中英

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.

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"
  ]
}
  {
  "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM