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"}
}
If you want to use a custom schema of your own that's part of your project, just click on your schema in Solution Explorer and then drag it to that dropdown list. Alternatively, you can use the $schema keyword in a JSON file to associate it with a schema.
We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format. Initially, we can create a POJO class and pass this instance as an argument to the put() method of Map class and finally add this map instance to the accumulateAll() method of JSONObject.
We can convert a Map to JSON object using the toJSONString() method(static) of org. json. simple. JSONValue.
JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.
You can:
{
"type": "object",
"properties": {
"itemType": {"$ref": "#/definitions/mapInt"},
"itemCount": {"$ref": "#/definitions/mapInt"}
},
"definitions": {
"mapInt": {
"type": "object",
"additionalProperties": {"type": "integer"}
}
}
}
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"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With