28

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"
    }
}
3
  • By defining the json shcema, are you asking how to write a class that represents the json? Commented Nov 22, 2016 at 19:45
  • @J.West No Schema in the form of JSON Commented Nov 22, 2016 at 19:52
  • Is this the example you want to refer to? Commented Nov 22, 2016 at 20:18

4 Answers 4

40

You can:

{
  "type": "object",
  "properties": {
    "itemType": {"$ref": "#/definitions/mapInt"},
    "itemCount": {"$ref": "#/definitions/mapInt"}
  },
  "definitions": {
    "mapInt": {
      "type": "object",
      "additionalProperties": {"type": "integer"}
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

How do we specify which of the two should be treated as "key" here ?
Keys will always be strings 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.
I am searching for how to represent Map<number, number>. I control server and client and thus serializers, but can't figure out how to define the key as a number, any help?
@ttugates You won't be able to do that in JSON schema since it's that doesn't conform to the JSON specification
8

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

Comments

2

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/

1 Comment

Do this also creates and additionalProperties variable
-4
{
    "type": "array",
    "maxItems": 125,
    "items": {
        "type": "array",
        "items": [
            { // key schema goes here },
            { // value schema goes here }
        ],
        "additionalItems": false
    }
}

1 Comment

This defines an array of pairs, not a map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.