museshad
museshad

Reputation: 498

Define JSON schema for dynamic array of dictionaries

I want to define the JSON schema for the below JSON. My confusion is around how to define the array of country dictionaries ("country_details"). The list of countries (US, IN) is dynamic and not known in advance.

{
    "map_id": 123456789,
    "country_details": {"US": [1, 2, 3], "IN": [4, 5, 6]}
}

Upvotes: 0

Views: 371

Answers (1)

CS.
CS.

Reputation: 786

If you want to setup country_details to be an object where each property (country) would have an array of numbers as its value, additionalProperties/unevaluatedProperties might help. Use size to prevent an empty object.

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "title": "The root schema",
  "description": "The root schema comprises the entire JSON document.",
  "default": {},
  "examples": [
    {
      "map_id": 123456789,
      "country_details": {
        "US": [1, 2, 3],
        "IN": [4, 5, 6]
      }
    }
  ],
  "required": [
    "map_id",
    "country_details"
  ],
  "properties": {
    "map_id": {
      "$id": "#/properties/map_id",
      "type": "integer",
      "title": "The map_id schema",
      "description": "An explanation about the purpose of this instance.",
      "default": 0,
      "examples": [
        123456789
      ]
    },
    "country_details": {
      "$id": "#/properties/country_details",
      "default": {},
      "description": "An explanation about the purpose of this instance.",
      "title": "The country_details schema",
      "type": "object",
      "examples": [
        {
            "US": [1, 2, 3]
        }
      ],
      "minProperties": 1,
      "unevaluatedProperties": {
        "type": "array",
        "items": {
          "type": "number",
          "additionalItems": true
        }
      }
    }
  },
  "additionalProperties": true
}

Upvotes: 0

Related Questions