skip to Main Content

If I have an object like this:

{
  "name": "John",
  "details": "{"age": 30, "email": "[email protected]"}"
}

Is it possible to describe nested JSON in "details" with a schema inside of an outer schema?

I imagine something like this:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "details": {
      "type": "string",
      "contentMediaType": "application/json", 
      "schema": {
        "type": "object",
        "properties": {
           "age": {
             "type": "number"
           },
           "email": {
             "type": "string"
           }
        }
      }
    }
  }
}

2

Answers


  1. No, the details is only validated as a string. It’s not possible to apply further schema constraints on the string value in this scenario.

    Login or Signup to reply.
  2. Yes, if you are using the latest draft, 2020-12. See section 8. A Vocabulary for the Contents of String-Encoded Data. You already have the contentMediaType; this draft adds contentSchema.

    Note, however, that you will find very limited support in tooling for validating the decoded JSON against this schema. The spec states "[these keywords] do not function as validation assertions; a malformed string-encoded document MUST NOT cause the containing instance to be considered invalid." So, yes, you can describe the contents of the JSON string, but that description will not be validated unless your application takes further steps to perform that validation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search