4

I have the following example JSON, for which I want to write a JSON Schema. The constraint is that property2 holds a list of string, which are dynamic, depending on the dataset. And in the property3 object some of the properties are named afters those strings.

 {
   "property1": "value",
   "property2": ["value1","value2","value2"],
   "property3": {
                   "title": "test",
                   "value1": "hello",
                   "value2": "world"
    }
 }

A JSON-Schema might look like this, but I don't know how to describe that the dynamic properties. Is that possible?

 {
   "title": "Test Object",
   "type": "object",
   "properties": {
              "property1": {
                     "type": "string"
               }
              "property1": {
                     "type": "array"
               }
              "property3": {
                     "type": "object",
                     "properties": {
                               "title": {
                                     "type": "string"
                               }
                               [ Something is missing here ]
                      }
               }
    }
 }

1 Answer 1

1

You can not relate property values to property keys with Json-Schema (as of Draft v4).

You can:

  • Define property2 as an schema and reference it (#ref) from property3 (dependencies,allOf, anyOf, additionalProperties...). If these property names are very dynamic in your case you might build this schema on the fly.
  • Use patternProperties in order to retrict valid property names in property3 to some fixed regex.
Sign up to request clarification or add additional context in comments.

Comments

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.