How to define JSON Schema for Map<String, Integer>?

Newbie picture Newbie · Nov 22, 2016 · Viewed 19.6k times · Source

I have a json :

{
"itemTypes": {"food":22,"electrical":2},
"itemCounts":{"NA":211}
}

Here the itemTypes and itemCounts will be common but not the values inside them (food, NA, electrical) which will be keep changing but the will be in the format : Map<String, Integer>

How do I define Json Schema for such generic structure ?

I tried :

"itemCounts":{
      "type": "object"
    "additionalProperties": {"string", "integer"}

    }

Answer

esp picture esp · Nov 22, 2016

You can:

{
  "type": "object",
  "properties": {
    "itemType": {"$ref": "#/definitions/mapInt"},
    "itemCount": {"$ref": "#/definitions/mapInt"}
  },
  "definitions": {
    "mapInt": {
      "type": "object",
      "additionalProperties": {"type": "integer"}
    }
  }
}