Thursday, January 21, 2016

JSON Mesasge Structure

In this blog I am going to write about JSON message. A json message is very similar to a JavaScript objects/arrays. Below are few rules I can define for a JSON message

1.       Message should start with a curly braces to represent a JSON object or square bracket to represent JSON Array.

Simplified JSON object is
{“key1”: value, “key2”: value} //you can add any number of key-value pair

Simplified JSON array could be
[value1, value2, value3] // you can add any number of values

2.       As per point 1, we have keys and values in a JSON message.

3.       Values can be null, primitive (string, Boolean, number), object or collection.

4.       Null value should be clearly represented by keyword null. No need of any double quotes.

5.       Any String value in message must be within double quotes

6.       Any Boolean value must be without double quote. Only two value expected true/false

7.       Any number value must be without double quote. It must be a number.

8.       Any object value must be surrounded by curly brackets { “key” : value, “key2”: value}. Object should have key/value pair. Key is a string so its name should be in double quotes. Key value must be separated by colon (:). Value could be null, primitive (string, Boolean, number), object {…} or collection [value, value, value]. Comma should be used to separate two key/value combination.

9.       Any collection value must be surrounded by square bracket […]. It can only have values in it. Effectively structure would be like [value1, value2, value3]. Values again could be null, primitive, object or collection.


Sample JSON object
    { “name”: “Development”, “dept-id”: 100,
“employees”: [
                {“first-name”: “Sanjeev”, “middle-name”: null, “empid”: 100, “isActiveEmployee”: true},
{“first-name”: “Sachin”, “middle-name”: null, “empid”: 101, “isActiveEmployee”: false},
         ]
    }

Sample JSON Array object:
[
                {“first-name”: “Sanjeev”, “middle-name”: null, “empid”: 100, “isActiveEmployee”: true},
{“first-name”: “Sachin”, “middle-name”: null, “empid”: 101, “isActiveEmployee”: false},

         ]