25

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"}

    }
Denis Barmenkov
  • 2,276
  • 1
  • 15
  • 20
Newbie
  • 691
  • 4
  • 16
  • 28

4 Answers4

38

You can:

{
  "type": "object",
  "properties": {
    "itemType": {"$ref": "#/definitions/mapInt"},
    "itemCount": {"$ref": "#/definitions/mapInt"}
  },
  "definitions": {
    "mapInt": {
      "type": "object",
      "additionalProperties": {"type": "integer"}
    }
  }
}
esp
  • 7,314
  • 6
  • 49
  • 79
  • 1
    How do we specify which of the two should be treated as "key" here ? – Sachin Jain Jan 30 '18 at 09:54
  • 2
    Keys will always be `string`s in JSON. both `itemType` and `itemCount` in this example are maps of "string" to "integer". Also note that you don't have to use the `definitions` thing in order to make a map of string to int -- that's just a shortcut used in this example to deduplicate definitions. – Jared Forsyth Nov 27 '18 at 13:08
  • I am searching for how to represent `Map`. I control server and client and thus serializers, but can't figure out how to define the key as a number, any help? – ttugates Aug 08 '19 at 13:12
  • 1
    @ttugates You won't be able to do that in JSON schema since it's that doesn't conform to the JSON specification – Brendan Samek Nov 26 '19 at 17:51
6

The question is kind of badly described, let me see if I can rephrase it and also answer it.

Question: How to Represent a map in json schema like so Map<String, Something>

Answer:

It looks like you can use Additional Properties to express it https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties

{
  "type": "object",
  "additionalProperties": { "type": "something" }
}

For example let's say you want a Map<string, string>

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

Or something more complex like a Map<string, SomeStruct>

{
  "type": "object",
  "additionalProperties": { 
    "type": "object",
    "properties": {
      "name": "stack overflow"
    }
  }
}
franleplant
  • 629
  • 1
  • 7
  • 17
0

using "additionalProperties" has 2 problems

  1. It creates additional class that represents Object or Int Map which internally has additionalProperty as member to be used as Map
  2. because its additionalProperties, it always has @JsonIgnore so it wont appear while serialization.

so In my opinion, The right approach would be to use direct Java Type.

"properties": {
  "myFieldName": {
    "existingJavaType" : "java.util.Map<String,String>",
    "type" : "object"
  }
}

ref: https://www.jsonschema2pojo.org/

Jay99
  • 81
  • 2
  • 8
-4
{
    "type": "array",
    "maxItems": 125,
    "items": {
        "type": "array",
        "items": [
            { // key schema goes here },
            { // value schema goes here }
        ],
        "additionalItems": false
    }
}
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
swarnim gupta
  • 213
  • 1
  • 5