Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create JSON Schema for Name/Value structure?

My problem is that i am serializing the content of map to JSON.

In the output (JSON), i have object that follow key/name syntax rule.

The key is created from map key, and the name from the value.

Model Example:

  class Storage {        Map<String,String> values = new HashMap<>();        {          map.put("key1","key1");          map.put("key2","key2");          map.put("key3","key3");       }      } 

JSON Example object:

{   key1=value1,   key2=value2,   key3=value3 } 

JSON Schema:

{   "name": "storage",   "description": "Store of key values",   "properties": {     // How can we describe the properties if we do not know the name ?    } } 

The issue is that i do not know what the values will be but i know that they will be some.

Can you help me to provide me the full definition of schema?


Disclaimer:

I know that this can be also serialized as

 {     values: [        {key="key1", value="value1"},        {key="key2", value="value2"},        {key="key3", value="value3"}     ]  } 

but is do not want to have array in the JSON.

like image 432
Damian Leszczyński - Vash Avatar asked Mar 06 '14 15:03

Damian Leszczyński - Vash


People also ask

How do I write a JSON Schema?

To begin creating a schema, add a JSON or JSON Schema file to your project. At the top of the file, you can specify the schema's id, the schema that should be used to validate the format of your schema, and a descriptive title.

What is JSON Schema example?

JSON Schema ExampleYou will use this to give a title to your schema. A little description of the schema. The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. Defines various keys and their value types, minimum and maximum values to be used in JSON file.


1 Answers

Assuming your validator supports it you can use patternProperties.

For the schema...

{   "title": "Map<String,String>",   "type": "object",   "patternProperties": {     ".{1,}": { "type": "string" }   } } 

...and the document...

{     "foo":"bar",     "baz":1 } 

...the value of property foo is valid because it is a string but baz fails validation because it is a number.

like image 192
McDowell Avatar answered Sep 28 '22 19:09

McDowell