Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe JSON data in a spec?

Tags:

json

What is the best way to describe JSON data in a spec?

In the past I've used examples with 'wordy' descriptions, but it feels imprecise.

There seems to be a nascent JSON schema standard, but it doesn't look like a hugely active project. Any other ways?


(Update) After thinking about this for several days I like bmargulies suggestion around using a conversion convention. Since the JSON documents in this case our coming out of .NET web services I am going to simply document the schema with C# class syntax. This may not be totally rigourous, but everyone involved will understand it and coupled with the examples will get the message across as quickly as possible.

like image 965
Rob Walker Avatar asked Jul 09 '10 14:07

Rob Walker


People also ask

How would you describe a JSON object?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.

How is JSON data represented?

JSON - Syntax Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma).

What is JSON describe it with proper example?

JSON is a file format used to store information in an organized and easy-to-access manner. Its full form is JavaScript Object Notation. It offers a human-readable collection of data that can be accessed logically. Its filename extension for written programming code is .

What should JSON data look like?

Each key-value pair is separated by a comma, so the middle of a JSON looks like this: "key" : "value", "key" : "value", "key": "value" . In our example above, the first key-value pair is "first_name" : "Sammy" . JSON keys are on the left side of the colon.


2 Answers

I would recommend my js-schema JavaScript library. The primary motivation behind it was the same what you describe in the question. It is a simple and easy to understand notation to describe JSON schemas (or specification, if you want).

An example schema described in JSON Schema:

{   "type":"object",   "properties":{     "id":{       "type":"number",       "required":true     },     "name":{       "type":"string",       "required":true     },     "price":{       "type": "number",       "minimum":0,       "required":true     },     "tags":{       "type":"array",       "items":{         "type":"string"       }     }   } } 

and the same schema description with js-schema:

{   "id"    : Number,   "name"  : String,   "price" : Number.min(0),   "?tags" : Array.of(String) } 

The library is able to validate object against schemas, generate random objects conforming to a given schema, and serialize/deserialize to/from JSON Schema.

like image 91
molnarg Avatar answered Oct 18 '22 16:10

molnarg


I know this is an older question, but it might be useful to someone else: When looking for methods to describe JSON-data I stumbled upon Orderly. Here's the abstract right of the front page:

Orderly is a textual format for describing JSON. Orderly can be compiled into JSONSchema. It is designed to be easy to read and write.

I can agree with that, but I have only tried it with relatively simple structures so far.

like image 24
Michael Sandino Avatar answered Oct 18 '22 17:10

Michael Sandino