JSON stands for javascript object notation and it is a text format that makes easy to share data between devices like clients and servers. JSON objects works like the way javascript object work, so infact it looks related to javascript objects but not to close. JSON available in many different languages like c, ruby, python and php. JSON is also an alternative to xml.



Advantages of JSON

Very easy to read and super easy to parse. JSON uses minimum formatting and few special characters in addition to the data. In the above  image you can see the json object with few key value pairs. The keys are in left hand side and values of keys in right hand side. For example key would be “full_name” and value be “Rahul”. Javascript can understand this as an object and we can access it. For example you can create object with this data and pass into a javascript object simply using javascript command call JSON.

Now we take a look at same data expressed in XML notation which is an other very popular format you can see that it is very larger than json. It means JSON data takes less space and loads faster in web application plus parsing an xml object is complicated and time consuming where as JSON can easily map into javascript object and consume less time.

Difference between JSON and Javascript

The Json declaration is a string with key and value pairs. In Json the keys are wrapped with double Quotes
The keys with single quotation or with out quotation is not valid json.
Json values have to be one of the six datatypes
Strings, Objects, Arrays, Numbers, booleans or null.
Here we created the javasript object
It is very close to json string but we can see some difference for example we are missing Quotation. Quotes on key not necessary.
Javascript object can be any data type including a function which u can do with json here we assigned a function to the key

JavaScript and JSON

JavaScript and JSON objects are really very easy to generate so take a look at some examples how Different types of information expressed in this format. The most common type of data is expressed in key and values pairs

Var info = {
"name" : "Faruk",
"age" :  24,
 "company" : www.studywithdemo.com
};

Usefull Points 

 Multiple values are separted by commas
 white space is for clearity and you can add any number of tabs, spaces.
The last element no need of comma.
Quotes are not required for numbers, true, false, null.

Retrieving Values

Once data is parse into a variable it becomes  a javascript object. Normally it is accessed though dot notation.

Console.log(info.name);
Faruk

You can also use the [] notation. This is specially used for the reason keys are not using valid javascript names

Objects are also contains the list which are like array elements

Var info = {
"name" : "Faruk",
"age" :  24,
 "company" : www.studywithdemo.com
“hobbies” : [
             “bloggging”
             “Singing” ]
};
Console.log(info.hobbies[1]);
Singing

The Value of any element can also be an object
Console.log(info.hobbies.link);

Note: while creating javascript objects we should take care about syntax.

0 comments:

Post a Comment

 
Top