Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you design JSON Schema for an arbitrary key?

I have the following JSON output data:

{    "label_name_0" : 0,    "label_name_5" : 3,    .    .    .    "label_name_XXX" : 4 } 

The output is simple: a key[1] name associated with integer value. If the key name doesn't change, I can easily come up with JSON Schema similar to this:

    {         "type": "array"         "title": "Data output",         "items" :{              "properties": {                 "label_name": {                    "type": "integer",                    "default": 0,                    "readonly": True,             }         }     }, 

Since the key name itself is not known and keep changing, I have to design schema for it. The only thing I know is that the key is string and not more than 100 characters. How do I define a JSON Schema for the key lable_name_xxx that keeps changing.

[1] Not sure if I am using the right terminology

like image 654
Cory Avatar asked Apr 25 '13 18:04

Cory


People also ask

How do I set a schema in JSON?

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. These are all defined using the keywords id, $schema and title, all of which are provided in the draft JSON Schema.

What is required in JSON Schema?

Required PropertiesThe required keyword takes an array of zero or more strings. Each of these strings must be unique. In Draft 4, required must contain at least one string.

What is JSON Schema?

JSON Schema is a JSON media type for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data.

What language is JSON Schema based on?

To define what JSON Schema is, we should probably first define what JSON is. JSON stands for “JavaScript Object Notation”, a simple data interchange format. It began as a notation for the world wide web. Since JavaScript exists in most web browsers, and JSON is based on JavaScript, it's very easy to support there.


1 Answers

On json-schema.org you will find something appropriate in the File System Example section. You can define patternProperties inside an object.

{     "type": "object",     "properties": {         "/": {}     },     "patternProperties": {         "^(label_name_[0-9]+)+$": { "type": "integer" }     },     "additionalProperties": false,  } 

The regular expression (label_name_[0-9]+)+ should fit your needs. In JSON Schema regular expressions are explicitly anchored with ^ and $. The regular expressions defines, that there has to be at least one property (+). The property consists of label_name_ and a number between 0 and 9 whereas there has to be at least one number ([0-9]+), but there can also arbitrary many of them.

By setting additionalProperties to false it constrains object properties to match the regular expression.

like image 79
Konrad Reiche Avatar answered Sep 22 '22 22:09

Konrad Reiche