JSON

        JSON Tutorial : Learn with easy explanation

What do you mean by JSON ?

JSON stands for JavaScript object notation.It is used to transfer data between the server and client.XML is also used for the same purpose but JSON has several advantages over XML.

It supports array,objects,strings,numbers and Boolean etc.


Let’s look at the code of JSON objects.It contains key-value pairs.

{
“student”:[
{
“name”:”suman”,
“lastname”:”kumar”,
“nickname”:”dil”
}
]
}

Features of JSON

1.It is a light-weight

2.It is easy to read and write

3.It is easy to parse over XML

4.It is human readable,text based.

5.it is  language independent  suitable almost all the language

JSON objects syntax rule

1.It contains the data in the form of key-value pairs.

2.Data are separated by the comma.

3.Curly braces contains objects.

5.Square bracket contains array.

Why use JSON over XML

1.Simple=JSON is very simple to read and write over XML. By which it is easy to parse the json objects

2.Faster=It is faster as compared to XML.It takes less time to parse the JSON objects so it saves time and memory.

3.Scable=it is language independent ie it is suitable with almost all programming languages

Syntax style of  JSON & XML

Here we will see the syntax of JSON and XML.With the help of syntax you can compare which is better.

Here we will store the 3 records firstname,lastname and nickname.

Syntax of JSON

{
“Students”:[ 
{
“firstname”:”suman”,”lastname=”kumar”,”nickname”=”dil”
},
{
“firstname”:”rahul”,”lastname”:”kumar”,”nickname”:”dev”
},
{
“firstname”:”amit”,”lastname”:”kumar”,”nickname”:”mithu”
}
]
}

Syntax of XML

<students>
<student>
<firstname>suman</firstname>
<lastname>kumar</lastname>
<nickname>dil</nickname>
</student>

<student>
<firstname>rahul</firstname>
<lastname>kumar</lastname>
<nickname>raj</nickname>
</student>

<student>
<firstname>amit</firstname>
<lastname>kumar</lastname>
<nickname>mithu</nickname>
</student>
</students>

As you can see from the above syntax of JSON and XML.JSON is a light-weight as compared to XML.

JSON Objects & JSON Array

JSON Objects

JSON objects holds the data in the form of key-value pairs.Key is represented as  string but value can be anything string,integer & Boolean etc.

The keys and values are separated by colon.

Each key/value separated by the comma.

{ (curly braces)= represents the objects in JSON.

Let’s see an  example

{
“Person”:
{
“name”:suman”,”age”:24,”salary”:20000”
}
}

JSON objects with string

{
“name”:”suman”
}

JSON objects with numbers

{
“age”=24
}

In the above example,a person is an object & name,age,salary and suman,24,20000 are the key and value respectively.

JSON Array

JSON array store multiple values.Values may be objects,string,integer,Boolean.

[ (square bracket) =represents the array in JSON. 

Let’s see an example

1.JSON Array of strings

[“suman”,”rahul”,”amit”,”raj”]

2.JSON  Array of Numbers

[1,2,3,4,5,]

3.JSON Array of Boolean

[true,false,true,false]

4.JSON Array of objects

{
“Person”:[
{“name”:”suman”,”age”:24,”salary”:20000
},
{
“name”:”rahul”,”age”:21,”salary”:25000
}
]
}

JSON vs XML

JSONXML

1.JSON stands for javascript object notation.


1.XML stands for extensible markup language.


2.It is simple to read and write


2.It is more secure as compared to JSON


3.It is less secure as compared to XML


3.It is less human readable


4.it is more human readable


4.It is a document oriented


5.It is a data oriented


5.It does not support array.

JSON Comments

JSON does not support any comment like programming language.

But we can do with the help of adding extra attributes into the JSON.

Example

{
“Person”:
{
“name”:”suman",
“age”:24,
“Comment”:”comment-value”
}
}

Here “comment” key can be used as a comment any line or sentence etc.

I hope that I explain all the concepts related to JSON with very easy and step by step.If you have any query then post me.