Introduction To JSON

This blog is a brief introduction of what JSON is and how we can store and retrieve the data in different ways, using JSON. 

Introduction to JSON 

  • JSON stands for JavaScript Object Notation.
  • JSON format was originally specified by Douglas Crockford.
  • JSON is a lightweight data interchange format and can be used to exchange/send /recieve the data between the Browser and the Server.
  • JSON is “self-describing” and easy to understand.
  • Language independent.
  • The file type for JSON files is ".json".
  • The MIME type for JSON text is "application/json". 

JSON Vs XML

JSON is more compact and faster than XML, as less data is transferred in the case of JSON. XML is  more difficult to parse than JSON. JSON is parsed into a ready-to-use JavaScript object.

For ex -for AJAX Applications, JSON is faster and easier than XML:

Using XML

  • Fetch an XML document.
  • Use XML DOM to loop through the document.
  • Extract the values and store in the variables.

Using JSON

  • Fetch a JSON string.
  • JSON.Parse the JSON string.

JSON Syntax

JSON data is written in name-value pairs.

{“name”:”value”}

For ex

//Json Object

var student = {"name":"Rashmi","gender":"F","dept":"IT","address":"Gurgaon"};

name, gender, dept, address are the properties of json object and seprated by comma(,).

In JSON, the values must be one of the data types given below.

  • null
  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean

The data can be stored in JSON format, as shown below.

  • JSON Object.
  • JSONArray.
  • Nested Json Object 

JSON Object

Here is an example on how to store student data in a JSON object.

  1. //Json Object  
  2. var studentJson = {  
  3.     "name""Rashmi",  
  4.     "gender""F",  
  5.     "dept""IT",  
  6.     "address""Gurgaon"  
  7. };  
  8. Read Data from json object:  
  9.     // To read property of studentJson  
  10.     var name = studentJson.name;  
  11. var gender = studentJson.gender;  
  12. var department = studentJson.dept;  
  13. var address = studentJson.address;  

JSON Array

JSON Array is used to store multiple JSON objects. For example, if we want to store more than one student's information in ‘studentJson’, we use JSON Array .

To create JSON Array, JSON object is wrapped in square brackets and separated by commas, as shown below.

  1. /Json Array  
  2. var studentJson = [{  
  3.     "name""Rashmi",  
  4.     "gender""F",  
  5.     "dept""IT",  
  6.     "address""Gurgaon"  
  7. }, {  
  8.     "name""Ashwani",  
  9.     "gender""M",  
  10.     "dept""Admin",  
  11.     "address""Gurgaon"  
  12. }, {  
  13.     "name""Sneha",  
  14.     "gender""F",  
  15.     "dept""HR",  
  16.     "address""Gurgaon"  
  17. }];  
  18.     // To read property of first student of array  
  19.     var name = studentJson[0].name; //results "Rashmi"  
  20. var gender = studentJson[0].gender; //results "F"  
  21. var department = studentJson[0].dept; //results "IT"  
  22. var address = studentJson[0].address; // results "Gurgaon"  

Nested JSON Object

Nested JSON object is an another way to store multiple JSON objects. To nest JSON object, we use additional “{}” instead of “[]”.

For example, we have three JSON objects “Rashmi” , ”Ashwani” , ”Sneha” enclosed within {}. To read the property of each, an object can directly use an object name with the property name, as described below.

  1. //Nested Json Object  
  2. var studentJson = {  
  3.     Rashmi: {  
  4.         "name""Rashmi",  
  5.         "gender""F",  
  6.         "dept""IT",  
  7.         "address""Gurgaon"  
  8.     },  
  9.     Ashwani: {  
  10.         "name""Ashwani",  
  11.         "gender""M",  
  12.         "dept""Admin",  
  13.         "address""Gurgaon"  
  14.     },  
  15.     Sneha: {  
  16.         "name""Sneha",  
  17.         "gender""F",  
  18.         "dept""HR",  
  19.         "address""Gurgaon"  
  20.     }  
  21. };  
  22. // To read property of json object "Rashmi"  
  23. var name = studentJson.Rashmi.name; //results "Rashmi"  
  24. var gender = studentJson.Rashmi.gender; //results "F"  
  25. var department = studentJson.Rashmi.dept; //results "IT"  
  26. var address = studentJson.Rashmi.address; // results "Gurgaon".  

Happy coding.