Open In App

JSON Interview Questions

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JSON or JavaScript Object Notationis a lightweight data format used to exchange data between the frontend and backend in web applications. It's very important to revise the important concepts to master JSON and ace interviews.

Here we are covering all the important questions starting from basic to advanced.

1. What is JSON?

JSON stands for JavaScript Object Notation. It is

  • A text-based data format is used to represent structured data.
  • Lightweight and easy to read and write.
  • Language-independent but heavily influenced by JavaScript syntax.

2. What are the basic data types in JSON?

JSON supports:

  • String: "Amit"
  • Number: 25 (includes integers and floating-point numbers)
  • Boolean: true, false
  • Array: ["item1", "item2"]
  • Object: {"key": "value"}
  • Null: null

3. Explain the structure and format of JSON.

The JSON format follows the structure of the JavaScript object. It contains the key-value pairs in the form of strings that can be transmitted from server to web app and vice-versa. The values stored in JSON can be retrieved in the same way as the values of JavaScript objects are retrieved.

{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}

4. Who created JSON?

Douglas Crockford was the first man who introduced and created the JSON format first in 2000.

5. What is the extension for the JSON files?

The JSON files can be created and stored using the .json extension. You can add the data in the form of JSON format using the key-value pairs in the form of strings.

6. How JSON data is transmitted form backend to frontend?

Data is sent as a JSON string using JSON.Stringify from backend to frontend over HTTP.

"{\"name\": \"Mohit\", \"age\": 30}"

Frontend parses the JSON string using JSON.Parse to use it as a JavaScript object.

const obj = {"name": "Mohit", "age" : 30}
let personName = obj.name;
let personAge = obj.age;

7. What are the features of JSON?

There are different features of JSON as listed below:

  • It is a light weight format that helps to optimize the performance of the web app.
  • It is independent of different programming languages.
  • It has a vast support from different programming languages.
  • The JSON format is very easy to understand.
  • It can represent the complex data structures.

8. How is JSON different from JavaScript Objects?

FeatureJSONJavaScript Objects
SyntaxText-based with strict rules (double quotes, no trailing commas).Can include functions, undefined, symbols, etc.
UsageData exchange format.Used to manipulate data in code.
ParsingRequires parsing via JSON.parse.Directly usable in JavaScript.

9. How do you convert JSON to a JavaScript object?

We can use JSON.parse()

JavaScript
const jsonS = '{"name": "Mohit", "age": 30}';
const obj = JSON.parse(jsonS);
console.log(obj.name);

Output
Mohit

10. How do you convert a JavaScript object to JSON?

Use JSON.stringify() to convert JavaScript Object to JSON

JavaScript
const obj = { name: "Mohit", age: 30 };
const jsonS = JSON.stringify(obj);
console.log(jsonS);

Output
{"name":"Mohit","age":30}

11. What is the role of JSON.stringify parameters?

JSON.stringify(value, replacer, space):

  • value: The object to stringify.
  • replacer: A function or array for filtering keys.
  • space: Adds indentation for readability.
JavaScript
const obj = { name: "Mohit", age: 30, city: "Delhi" };
console.log(JSON.stringify(obj, null, 2)); 

Output
{
  "name": "Mohit",
  "age": 30,
  "city": "Delhi"
}

12. Can JSON have functions?

No. JSON does not support functions or undefined values.

JavaScript
const obj = { name: "Mohit", greet: () => "Hello!" };
console.log(JSON.stringify(obj)); //will give only name

Output
{"name":"Mohit"}

13. How can you handle circular references with JSON.stringify()?

Circular references cause errors. Use a custom replacer:

JavaScript
const obj = {};
obj.self = obj;

const jsonS = JSON.stringify(obj, (key, value) => {
    if (key === "self") return undefined;
    return value;
});
console.log(jsonS);

Output
{}

14. How can you clone an object using JSON?

JSON can be used to create a deep copy (limitations apply):

JavaScript
const jObj = { name: "Mohit", age: 30 };
const clone = JSON.parse(JSON.stringify(jObj));
clone.name = "Amit";
console.log(jObj.name);
console.log(clone.name); 

Output
Mohit
Amit

15. How to create a JSON array?

A JSON array is similar to arrays in other programming languages, containing a collection of multiple items. These items can be of any data type supported by JSON, such as strings, numbers, booleans, null, objects, or even nested arrays.

["value1", 12, true, null]
  • JSON arrays are enclosed in square brackets [ ].
  • Items in the array are separated by commas.
  • JSON arrays can hold mixed data types

16. How to access JSON object values using dot notation.

You can follow the below syntax to retrieve values of JSON object using dot notation.

JavaScript
const JObj = {
    "name": "GFG",
    "est": "2009",
}
console.log(JObj.name)
console.log(JObj.est)

Output
GFG
2009

17. How to access JSON object values using square brackets?

You can follow the below syntax to retrieve values of JSON object using square brackets.

JavaScript
const JObj = {"name": "GFG", "est": "2009"}
console.log(JObj["name"])
console.log(JObj["est"])

Output
GFG
2009

18. Explain JSONP and how it is different from JSON?

JSONP stands for JSON with Padding. It is a method for sending the JSON data with padding to avoid cross domain restrictions. JSONP is different from JSON, because it allows the cross-origin requests.

19. List different browsers that support JSON.

JSON is supported by all the modern browsers. As we know, JSON follows the JavaScript syntax, hence every browser has built-in support for it. The below browsers support JSON:

  • Google Chrome
  • Mozilla Firefox
  • Safari
  • Microsoft Edge
  • Opera, etc.

20. Explain JSON schema.

JSON schema is a content specification language defined to validate the structure and the format of JSON data or string. It defines the basic structure, format and important constraints of JSON data.

21. How to write JSON in an HTML document?

You can write or include the JSON data inside and HTML document using a <script> tag with type attribute by passing a value as application/json to it.

<script type="application/json">
// JSON data
</script>

22. What are the methods used to encode and decode JSON in PHP?

We can use the below methods to encode and decode JSON in PHP:

  • json_encode(): This method is used to encode JSON in PHP. It takes the data in PHP array format and converts it into JSON data.
  • json_decode(): It is used to decode JSON in PHP. It takes a JSON string as input and returns corres[ponding PHP array.

23. What is the MIME type for JSON?

The MIME type for JSON is application/json. You can pass this type as an value to the type attribute inside the script tag to include JSON into your HTML document.

24. What are the benefits of using JSON?

The below list contains some benefits of using JSON:

  • It is easy to implement and understand.
  • It can be used with any language due to its language independency.
  • It's a very lightweight data interchange format on the network.
  • It's an alternative of XML.

25. Explain disadvantages of using JSON.

There are some disadvantages of using JSON:

  • It does not have any Document Type Definition.
  • It is not good for large scale data products.
  • There's no type definition of it.
  • It is not safe to use with untrusted services and browsers as it does not have a solid security mechanism.
  • It can output a complex data in case of large data set which may be difficult to understand.

26. What are the applications of JSON?

JSON can be used in different situations:

  • It serializes and transmits the structured data over a network connection.
  • There are multiple web services and APIs available on the internet that returns the data in JSON format when the data is fetched from them using JavaScript or any other language.
  • It transfers the data between the servers and web applications.

27. List some of the popular libraries for working with JSON in JavaScript.

Below are some of the popular libraries for working with JSON in JavaScript:

28. Differentiate between JSON and XML.

JSON and XML are the formats used to transfer data over the network. But there are some differences between them as listed in the below table:

JSON

XML

It stands for JavaScript Object Notation.

It stands for Extensible Markup Language.

It doesn't use any tag.

It uses end tag.

It allows to create arrays.

It does not have arrays.

JSON is simple and easy to read as well as write.

XML is a little complex as compare to JSON.

It does not have a robust and strong security mechanism.

It is much secure than JSON.

29. What are the similarities between JSON and XML?

There are some similarities between JSON and XML as listed below:

  • Both of them are used to transmit data to the network connection.
  • Both are simple and human-readable.
  • They are language independent.
  • Both of them supports hierarchical and nested structure i.e. values within values or objects with object.
  • They can be fetched by sending requests to the servers.

30. What is Serialization and Deserialization in JSON?

  • Serialization: It is the term refer to the process of transforming and object to the string.
  • Deserialization: It is the process of converting a string back into an object.

31. How to handle JSON parsing errors in JavaScript?

The JSON parsing errors can be handled using the try/catch block with the parsing statements in JavaScript.

32. Is it possible to create duplicate keys in an JSON object?

No, JSON does not allow to create duplicate keys in an object. If there's a duplicate key created in some scenario, then the last occurrence of that key will replace the previous occurrence.

33. Explain JSON web token and its implementation.

JSON web token or JWT is a self-contained and compact way to transmit the data securely between server and web app in JSON object format. It consist of three parts as the header, the payload, and the signature. All the parts of JWT are encode in base64 format which are concatenated to form a JWT.

34. How to transmit JSON data securely over the internet?

The JSON data can be securely transmitted over the internet using the HTTPS (Hyper Text Transfer Protocol Secure) protocol. It encrypts the data during its transmission over the internet.

35. What is JSON-LD and how it is different from JSON?

JSON-LD stands for JSON for Linking Data. It is a lightweight Linked Data format that is designed to integrate with the JSON based APIs that are already existing on he internet. It is different from regular JSON as it involves the additional context and semantics for the better data interoperability.

36. What is the purpose of toJSON() method in JSON?

The toJSON() method in JSON is used to convert the date returned by the Date constructor in the string format.

dateObj.toJSON();

37. What are JSON formatter and Validator?

The JSON formatter and Validators are the tools used to format and validate the JSON data. These tools convert the JSON data into a human-readable format by ensuring its readability, correctness, and adherence to JSON syntax.

38. Can JSON keys be numbers?

No, JSON keys must be strings.

Invalid

{ 123: "value" }

Valid

{ "123": "value" }

39. What is the difference between undefined and null in JSON?

  • undefined: Not supported in JSON.
  • null: Represents an empty value.
JavaScript
const obj = { name: "Mohit", value: null, key: undefined };
console.log(JSON.stringify(obj));

Output
{"name":"Mohit","value":null}

40. What are the alternatives of JSON?

There are some alternatives available for JSON as listed below:

  • XML (Extensible Markup Language)
  • Protocol Buffers (protobuf)
  • YAML
  • MessagePack
  • BSON (Binary JSON)
  • CBOR (Concise Binary Object Representation)

Next Article

Similar Reads