'How to define JSON Schema for Map<String, Integer>?
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"}
}
Solution 1:[1]
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"
}
}
}
Solution 2:[2]
"properties": {
"myFieldName": {
"existingJavaType" : "java.util.Map<String,String>",
"type" : "object"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | franleplant |
| Solution 2 | Jay99 |
