简体   繁体   中英

Schema validation for Json array of objects using Java

I am looking for JSON Schema validation in java code for an array of data.

My JSON Schema is

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "acc": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "AccId": {
              "type": "integer"
            },
            "accName": {
              "type": "string"
            },
            "accSysName": {
              "type": "string"
            },
            "accLName": {
              "type": "string"
            }
           },
          "required": [
            "AccId",
            "accName",
            "accSysName",
            "accLName"
          ]
        }
      ]
    }
  },
  "required": [
    "acc"
  ]
}

while my JSON response for 1 record is

{
  "acc": [
    {
      "AccId": 123,
      "accName": "test",
      "accSysName": "ABC",
      "accLName": "test"
    }
  ]
}

My code from http://wilddiary.com/validate-json-against-schema-in-java/ works fine in the above case but in the below case it doesnt go for the check of the second record , that is when JSON response has multiple objects.

JSON response for multiple records:

{
  "acc": [
    {
      "AccId": 123,
      "accName": "test",
      "accSysName": "Abc",
      "accLName": "test"
    },
    {
      "pqr": 456,
      "qwe": "test2",
      "accSysName": "ghu",
      "accLName": "test3"
    }
  ]
}

I tried with additionalProperties / additionalItems: false as well, yet it didn't work for either of them which I had checked for few old posts in Stack Overflow as well but didn't get the desired result.

Is there any other way in which it can work?

To validate your json response . Copy and paste your json response in link below https://jsonformatter.curiousconcept.com/

It will tell you that your json is valid or not

You are using the wrong form of items . The form you want should look like this.

{
  "type": "array",
  "items": { "type": "string" }
}

This will validate that all items are strings

["foo", "bar"]

The other form of items describes something like a tuple.

{
  "type": "array",
  "items": [
    { "type": "string" }
    { "type": "integer" }
  ]
}

This validates that the first item of the array is a string and the second item is an integer. Anything passed the second item is ignored (unless you use additionalItems )

["foo", 1]

So, in your schema, you've defined a tuple of one element and the rest are ignored. Switch to the first form and it will work as you expect.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "acc": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "AccId": {
            "type": "integer"
          },
          "accName": {
            "type": "string"
          },
          "accSysName": {
            "type": "string"
          },
          "accLName": {
            "type": "string"
          }
        },
        "required": [
          "AccId",
          "accName",
          "accSysName",
          "accLName"
        ]
      }
    }
  },
  "required": [
    "acc"
  ]
}

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