Wednesday, 13 May 2015

What is JSON ?

Here I explained what is JSON and what is the use of it with examples

What is JSON?

JSON stands for JavaScript Object Notation
JSON is a lightweight data-interchange format
JSON is language independent 
JSON is "self-describing" and easy to understand



Keys and Values

The two primary parts that make up JSON are keys and values. Together they make a key/value pair.
Key:  A key is always a string enclosed in quotation marks.
Value:  A value can be a string, number, boolean expression, array, or object.

Key/Value Pair:  A key value pair follows a specific syntax, with the key followed by a colon                                          followed by the value. Key/value pairs are comma separated.

Types of Values

Array:  An associative array of values.
Boolean: True or false.
Number:  An integer.
Object:  An associative array of key/value pairs.
String:  Several plain text characters which usually form a word.

Syntex:-

var json = {"Name1":"Value1", "Name2":"Value2"}

OR

var json = {"Key1":"Value1", "Key2":"Value2"}

JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner. In a nutshell, it gives us a human-readable collection of data that we can access in a really logical manner.
It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language
JSON uses JavaScript syntax, but the JSON format is text only, just like XML.
Text can be read and used as a data format by any programming language.

The following JSON example defines an Product object, with an array of 4 Product records:

Simple JSON Example
This is sample example of JSON object enclosing by curly braces

Var json=  {
                    "ProductId":"101",
                    "ProductName":"Redmi Note",
                     "Price":"9000"
                  };


JSON Array Example
Some times we need to store more records in a single variable to do this we enclose multiple objects by square braces called JSON Array.

Var json= {"ProductArray":[

                                            {"ProductId":"101", 
                                             "ProductName":"Redmi Note",
                                             "Price":"9000"},

                                              {"ProductId":"102",
                                                "ProductName":"Redmi 1S",
                                         "Price":"6000"},

                                              {"ProductId":"103",
                                                 "ProductName":"Redmi 3",
                                                 "Price":"14000"},

                                                {"ProductId":"104",
                                                 "ProductName":"Redmi 4G",
                                                  "Price":"10000"}
                                           ]}

in the above example json is variable that contains an Array of product called ProductArray.

Nested JSON

In Simple JSON Array each Key have associated with a value but in Nested JSON Array
Some of keys again contains a JSON object see in below example.

Var json=  {
                       "CellPhone": {"ProductId":"101",
                                               "ProductName":"Redmi Note",
                                               "Price":"9000"
                                                },
 
                       "Accessories": {"ProductId":"201",
                                                   "ProductName":"Earphones with Mic",
                                                   "Price":"1100"
                                                  }
};





No comments:

Post a Comment