Questions on JSON - Quick JavaScript Interview Questions (2015)

Quick JavaScript Interview Questions (2015)

Chapter 4. Questions on JSON

Q. What is JSON ?
The JSON text format is syntactically identical to the code for creating JavaScript objects. JSON is only a subset of JS object literal notation, but apart from looking similar, they have nothing in common. JSON is used as data exchange format, like XML. JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Q. Why does Google prepend while (1); to their JSON responses? ANSWER
It prevents JSON hijacking. This is to ensure some other site can't do nasty tricks to try to steal data. By replacing the array constructor, then including this JSON URL via a <script> tag, a malicious thirdparty site could steal the data from the JSON response. By putting a while(1); at the start, the script will crash instead. A same-site request using XHR and a separate JSON parser, on the other hand, can easily ignore the while(1); prefix.

Q. What is JSONP ?
JSONP stands for “JSON with Padding”. It means JSON data wrapped in a function call. A callback (processing/padding) function already defined in the Web page at the time of requesting remote JSON data.

Example :
The below JavaScript code shows a callback function paddingFunction() for a remote URL
//abcdurl .
function paddingFunction(data){

console.log("response data processing code")
var script = document.createElement('script');
script.src = '//abcdurl?callback=paddingFunction'
document.getElementsByTagName('head')[0].appendChild(scrip t);

Q. Why use JSON over XML ?
The following points are in favor of JSON over XML format:

JSON can contain Integer, String, List, Arrays. XML is just nodes and elements that needs to be parsed into Integer, String and so on before it is used by your application. JSON is smaller, faster and lightweight compared to XML. So for data delivery between servers and browsers, JSON is a better choice.
JSON is best for use of data in web applications from web services because of JavaScript which supports JSON. The overhead of parsing XML nodes is more compare to a quick look up in JSON.
For a newbie, JSON might look complex to read and understand because of its structure unless it is well written with all brackets and commas properly indented. JSON can be mapped more easily into object oriented system.
JSON and XML both use Unicode and thus support Internationalization.
JSON is a better data exchange format. XML is a better document exchange format.
JSON is less secure because of absence of JSON parser in browser.
If the data is in XML, you can write an XSLT template and run it over the XML to output the data into another format: HTML, SVG, plain text, comma-delimited, even JSON. When you have data in JSON, it’s pretty much stuck there. There’s no easy way to change it into another data format. So here, XML scores over JSON.

Q. What is MIME type for JSON?
The MIME type for JSON is application/json.

Q. What data types are supported by JSON?
Different data types supported by JSON are Number, String, Boolean, Array, Object, null. The following code shows a JSON object containing different data types:

{ name:"Sandeep",
isPassed: true,
subject: ["Computer", "Algebra"],
address: { city:"Bangalore", country:"India"}, email: null

The following screenshot shows the above JSON object in a chrome developer console with all properties listed in key value pair.

Q. What is the role of JSON.stringify() method?
JSON.stringify() turns an object into a JSON text and stores that JSON text in a string. The following screenshot shows the use of stringify() method:

Q. What is the role of JSON.parse() method?
The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing. The following screenshot shows the use of JSON.parse() method to convert the student1 JSON string to a JSON object.

Q. What is the complete syntax of JSON.parse() method? ANSWER
The complete syntax for JSON.parse() method is as follows:

JSON.parse(text[, reviver])

The details of the above syntax are as follows:
Text: It represents the string to parse as JSON.
Reviver: It represents a function, prescribes how the value originally produced by parsing is transformed, before being returned.

Q. How to convert date object to JSON?
The toJSON() method can convert the date object to JSON. The following screenshot shows the use of toJSON() method for a date object in chrome console.