1

Can i create json schema for validitaion this object without redesigning json in other form?

{      
        "itemColors": {
          "40": "#12ffd6",
          "69": "#f90861",
          "185": "#063ac3"
        },
        "itemVisible": {
          "32": true,
          "33": true,
          "34": true,
          "36": true,
          "37": true,
          "38": true,
          "39": true,
          "40": true,
          "41": true,
          "55": true,
          "56": true,
          "69": true,
          "185": true,
          "187": true,
          "196": true,
          "197": true,
          "198": true
        } 

}

Objects can have different numbers of properties.

Values for ItemColors:

  • must be of type string
  • do not have to be unique
  • can be any number of them

Same thing for itemVisible but values must be of type boolean

5
  • You need to provide more detail. Yes, you can create a schema for this specific JSON document; however, I suspect you don't want that. What is it that you actually want to validate? Is it enough to validate that the values for itemColors should be of type string? That they should be unique? That there should be a maximum of 3 of them? Without this information we cannot help you and your post will probably be closed. Commented Apr 12, 2017 at 9:09
  • Post was corrected Commented Apr 12, 2017 at 9:53
  • What about itemVisible? Commented Apr 12, 2017 at 9:54
  • the same, but with boolean values Commented Apr 12, 2017 at 9:57
  • Fixed that for you Commented Apr 12, 2017 at 10:00

1 Answer 1

1

You can specify the schema of unspecified properties using "additionalProperties" :

{
   "properties" : {
       "itemColors" : {
            "type" : "object",
            "additionalProperties" : {
                "type" : "string"
            }
       },
       "itemsVisible" : {
            "type" : "object",
            "additionalProperties" : {
                "type" : "boolean"
            }
       }
   }
}

Read more here: https://spacetelescope.github.io/understanding-json-schema/reference/object.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This is exactly what i searched

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.