JSON Schema with unknown property names

Thami Bouchnafa picture Thami Bouchnafa · Aug 17, 2015 · Viewed 18k times · Source

I want to have a JSON Schema with unknown property names in an array of objects. A good example is the meta-data of a web page:

      "meta": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "unknown-attribute-1": {
              "type": "string"
            },
            "unknown-attribute-2": {
              "type": "string"
            },
            ...
          }
        }
      }

Any ideas please, or other way to reach the same?

Answer

pink spikyhairman picture pink spikyhairman · Feb 20, 2017

Use patternProperties instead of properties. In the example below, the pattern match regex .* accepts any property name and I am allowing types of string or null only by using "additionalProperties": false.

  "patternProperties": {
    "^.*$": {
      "anyOf": [
        {"type": "string"},
        {"type": "null"}
      ]
    }
  },
  "additionalProperties": false