JSON Interview Questions and Answers

JSON stands for JavaScript Object Notation. It is an open-standard file format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types.It is a text format that is completely language independent.


1) Mention what is JSON?

JSON is a simple data exchange format.  JSON means JavaScript Object Notation; it is language and platform independent.


2) Explain what is JSON objects?

An object can be defined as an unordered set of name/value pairs.  An object in JSON starts with {left brace} and finish or ends with {right brace}.  Every name is followed by: (colon) and the name/value pairs are parted by, (comma).

Syntax:

{"firstName":"John", "lastName":"Anderson"}


3) Is Json Is A Language?

JSON is a data format. It could be classified as a language, but not a programming language. Its relationship to JavaScript is that it shares its syntax (more or less) with a subset of JavaScript literals.


4) Why Json Format Is Better Than Xml?

XML is much more difficult to parse than JSON.
JSON is parsed into a ready-to-use JavaScript object.

For AJAX applications, JSON is faster and easier than XML Like mentioned in below points:

Using XML

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

Using JSON

  1. Fetch a JSON string
  2. JSON.Parse the JSON string


5) How to convert Javascript object into Json String?

By using JSON.stringify() function we can convert  Javascript object into Json String.

Example : 

var myObj = { name: "John", age: 31, city: "New York" };
var myJSON = JSON.stringify(myObj);


6) How to Convert a string written in JSON format, into a JavaScript object?

By using JSON.parse() function we can convert Json String into Javascript Object.

Syntax:

var myJSON = '{"name":"John", "age":31, "city":"New York"}';
var myObj = JSON.parse(myJSON);

 


7) How Using JSON you can store and retrieve data into Local Storage?

When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.

JSON makes it possible to store JavaScript objects as text.

Syntax:

<script>
var myObj, myJSON, text, obj;

// Storing data:
myObj = { name: "John", age: 31, city: "New York" };
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);

// Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;

</script>


8) What Kind Of Format Is Json, And What Does The Acronym Mean?

A lightweight data-interchange format. JavaScript Object Notation.


9) Why must one use JSON over XML?

Use JSON over XML Because:

  1. JSON doesn't use end tag
  2. JSON is shorter
  3. JSON is quicker to read and write
  4. JSON can use arrays

The biggest difference is:

 XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.


10) Mention what is the function used for encoding JSON in PHP?

For encoding JSON in PHP, json_encode () function is used.  This function returns the JSON representation of a value on success or false on failure.