Json Schema - how to express a field mixed Types (string and object)?

3.3k views Asked by At

I have a field in our data with multiple typing:

It could be type=string, which has the schema:

{"mixed_field" : {"type":"string"} }

Other times it could be type=object, the schema looks like:

{"mixed_field" : {
  "properties": {
    "access_token": {
      "type": "string"
    },
    "created_at": {
      "type": "integer"
    }
  },
  "type": "object"
  }
}

How do I express that "mixed_field" can be either type string or type object? Should I use the "oneOf" keyword as follows?

{
  "mixed_field": {
    "oneOf": [
      {
        "type": "string"
      },
      {
        "properties": {
          "access_token": {
            "type": "string"
          },
          "created_at": {
            "type": "integer"
          }
        },
        "type": "object"
      }
    ]
  }
}
1

There are 1 answers

1
esp On

You can use oneOf/anyOf or you can use "type": ["string", "object"], in case it is a string "properties" keyword will be ignored.