Skip to main content

JSON Methods & JavaScript

Data Types: JSON Methods

Explain what is JSON?

View Answer:
Interview Response: JSON or JavaScript Object Notation is a lightweight and straightforward data exchange format used across multiple language platforms, including JavaScript. JSON means JavaScript Object Notation, and it is language and platform-independent.

Technical Response: JSON is a simple and lightweight data exchange format used across multiple language platforms. JSON means JavaScript Object Notation, and it is language and platform-independent. JSON often gets used when data gets sent from a server to a web page, and it works both on the front and back end of development environments. Several built-in JavaScript methods convert JSON objects from and back to their original format.

Can you explain the role of the JSON.stringfy() method in JavaScript?

View Answer:
Interview Response: The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Syntax: JSON.stringify(value[, replacer[, space]])

  • The value to convert to a JSON string.
  • • The (optional) replacer is a function or array used to change the behavior of the stringification process.
  • • The (optional) space consists of a String or Number object that gets used to introduce white space into the output JSON string to improve readability. If this is a number, it denotes the number of white space characters to use, and the number is limited and defaults to 10 spaces.

Simple Example:

let student = {
name: 'John',
age: 30,
isAdmin: false,
courses: ['html', 'css', 'js'],
wife: null,
};

let json = JSON.stringify(student);

alert(typeof json); // we've got a string!

alert(json);

JSON Output:

{
"name": "John",
"age": 30,
"isAdmin": false,
"courses": ["html", "css", "js"],
"wife": null
}

What data types does JSON support in JavaScript?

View Answer:
Interview Response: JSON supports the following data types: Objects, Arrays, (primitive) strings, Boolean values, numbers, and null.

Code Example:

// a number in JSON is just a number
alert(JSON.stringify(1)); // 1

// a string in JSON is still a string, but double-quoted
alert(JSON.stringify('test')); // "test"

alert(JSON.stringify(true)); // true

alert(JSON.stringify([1, 2, 3])); // [1,2,3]

JSON is a data-only, language-independent specification. What are some JavaScript-specific object properties that JSON.stringify skips?

View Answer:
Interview Response: JSON.stringify() ignores several JavaScript-specific object properties, including function properties, Symbolic keys and values, and properties that hold a value of undefined.

Code Example:

let user = {
sayHi() {
// ignored
alert('Hello');
},
[Symbol('id')]: 123, // ignored
something: undefined, // ignored
};

alert(JSON.stringify(user)); // {} (empty object)

What are the known differences between JSON and JSONP?

View Answer:
Interview Response: The difference between JSON and JSONP is that JSON is a simple data format for communication between different systems, and JSONP is also known as JSON with Padding. JSONP is a methodology for using that format with cross-domain ajax requests while not being affected by same-origin policy issues. The critical thing to remember with JSONP is that it is not a protocol or data type, and it is just a way of loading a script on the fly and processing the script that gets introduced to the page. In the spirit of JSONP, this means introducing a new JavaScript object from the server into the client application/ script.

What is the third argument space used in the JSON.stringify() method?

View Answer:
Interview Response: The third argument of JSON.stringify(value, replacer, space) is the number of spaces to use for pretty object formatting.

Technical Response: The third argument of JSON.stringify(value, replacer, space) is the number of spaces to use for pretty formatting. Previously, all stringified objects had no indents and extra spaces. That is fine if we want to send an object over a network. The space argument is used exclusively for nice formatting. Remember that if you do not use the replacer, we should set it to null.

Code Example:

let user = {
name: 'John',
age: 25,
roles: {
isAdmin: false,
isEditor: true,
},
};

alert(JSON.stringify(user, null, 2)); // value: user, replacer: null, space: 2

JSON Indented 2 spaces:

{
"name": "John",
"age": 25,
"roles": {
"isAdmin": false,
"isEditor": true
}
}

JSON Indented 4 spaces:

// {
// "name": "John",
// "age": 25,
// "roles": {
// "isAdmin": false,
// "isEditor": true
// }
// }


Who is known as the father and creator of JSON?

View Answer:
Interview Response: Douglas Crockford is widely regarded as the "Father of JSON." Douglas Crockford designed the JSON format for the first time in 2000.

What is the extension that we use for JSON files?

View Answer:
Interview Response: The extension of a JSON file is ".json." Because JSON files are text-based, we can change or view the file content using any text editor, such as notepad or notepad++.

Can a comment be added inside a JSON file?

View Answer:
Interview Response: JSON does not support any comments. However, we can store comments in a Key or data object. We need to make sure that our application ignores the given data element during the processing of the JSON.

Code Example: Commenting in JSON (Technically, it can be done but is not supported)

{
"_comment1": "this is my comment", // <-- comment (not supported)
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
}

Can you explain the function and syntax of the JSON.parse() method in JavaScript?

View Answer:
Interview Response: The JSON.parse() method parses a string and returns a JavaScript object. The string must get written in JSON format. The JSON.parse() method can optionally transform the result with a function.

Technical Response: The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. We can use an optional reviver function to perform a transformation on the resulting object before our code returns it. JSON parse is the opposite of the stringify method, and it returns an Object, Array, string, number, a boolean, or null value corresponding to the given JSON object text. You should note that JSON.parse() does not allow trailing commas.

Code Example:

Syntax: JSON.parse(text[, reviver]);

// Simple Example: stringified array
let numbers = '[0, 1, 2, 3]';

numbers = JSON.parse(numbers);

alert(numbers[1]); // 1

// Example: Using the option second argument reviver
JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
console.log(key); // log the current property name, the last is "".
return value; // return the unchanged property value.
});

// 1
// 2
// 4
// 6
// 5
// 3
// ""
note

We should note that JSON.parse() does not allow trailing commas.