Q) What is JSON?

A) JSON stands for JavaScript Object Notation which is used to exchange data between browser and server. Its a format which hold the data in string format.


Q) What are the uses of JSON?

A) Below are uses/Characteristics of JSON:

  • It is mainly used to transmit data between a server and browser.
  • API’s use JSON format to provide data since it is very lightweight. It is also human readable and in simple format.
  • JSON makes it possible to store JavaScript objects as text.
  • JavaScript has a built in function to convert a string in JSON format into JavaScript objects.

Q) Explain the syntax of JSON?

A) JSON syntax is similar to JavaScript object syntax:
Data is represented in name/value pairs
To Hold an object it uses curly braces
And square brackets are used for Arrays.
Example:

Simple syntax:{ "Name":"InkFort" }

(Note: Both name and value should be strings in double quotes where as for JavaScript only value is in double quotes like Name:”Inkfort” )


Q) Explain basic example of JSON object

A) Below is the example of JSON object

{
"Company":{ "Name":"InkFort","ID":"IFID","Type":"Technical" }
}

//Below is simple syntax of array:
{
"InkFortTechnologies":["C Sharp","JSON","Javascript","SQL Server"]
}

Q) What are Nested JSON objects?

A) Any JSON object that has another JSON object is called Nested JSON object. Below is example:

var a ={
"EmpId":"101",
"EmpName":"MAK",
"EmpLanguages":{
"Lang1":"English",
"Lang2":"French",
"Lang3":"German"
}
}

Q) What is the difference between JSON and XML?

A) Following are the main difference betwen JSON and XML:

  • JSON is lightweight compared to XML since it doesn’t use end tag and is much shorter compared to XML
  • JSON is also quick to read when compared with XML
  • XML is more difficult to parse than JSON as JavaScript has ready to use functions to parse JSON object whereas for XML we need to fetch XML document, loop though document and extract values.

Q) How to parse JSON to JavaScript Object?

A) JSON.parse() can be used to convert JSON text to JavaScript Object.
Below is example:

'{"Name":"MAK","Age":"30"}'// This is basic json string which will be converted to JavaScript Object

var obj=JSON.parse('{"Name":"MAK","Age":"30"}');
alert("Name is "+ obj.name);

Q) What is JSON.stringify()?

A) JSON.stringify() is predefined function which is used to convert any JavaScript object to JSON string.

Example: Below syntax shows conversion of JavaScript object to JSON string

var JSObj=["C Sharp","Sql Server","ASP Web API"];

var JSONStringObj=JSON.stringify(JSObj);

Q) How to Parse Dates in JSON?

A) We cannot use date objects in JSON as only string is allowed.
But date can be written in string which later can be converted back to Date.

Below is the example:

var v='{"DateOfBirth":"1987-07-27"}'

var JObj=JSON.parse(v);

JObj.DateOfBirth=new Date(JObj.DateOfBirth);

Q) Can we loop through object properties of JSON object? If yes how?

A) Yes we can loop through JSON object properties using for in loop. Below is the example:

var a={"EmployeeName":"MAK", "EmpCode":"101","EmpDept":"Accounts"};

for (x in a)
{
document.getElementById("AnyDiv").innerHTML+=x;
}