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?
Feature | JSON | JavaScript Objects |
---|
Syntax | Text-based with strict rules (double quotes, no trailing commas). | Can include functions, undefined, symbols, etc. |
Usage | Data exchange format. | Used to manipulate data in code. |
Parsing | Requires 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);
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
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);
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);
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)
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"])
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)
Similar Reads
20 Smart Questions to Ask in an Interview
If you've ever faced any interview then you must have heard this line from the interviewer - Do you have any questions to ask? Indeed, an interviewer expects you to ask several worthwhile questions to determine your interest and zeal toward the job opportunity. Moreover, asking relevant questions to
8 min read
20 Smart Questions to Ask at the End of Job Interview
As it is truly said, asking the right questions at the right time can help you in making the right choices! Similarly, when you are appearing for a job interview, just answering the questions asked by the interviewer and leaving is not enough. You should do the smart work and ask a few important que
8 min read
Top HR Interview Questions and Answers (2025)
HR interviews can be daunting but they donât have to be. The bottom line in most hiring processes entails testing the personality of a candidate for their communication traits and company culture fit. Being at the initial or experienced levels of your career being prepared for commonly asked fresher
15+ min read
Common Interview Questions and Preparation Guide for 2024
A peal of nervous laughter, an anxious candidate aspiring for the job of their dreams. We've all been there at some point in our lives. These Common Interview Questions and Preparation guide will help you in your next interview. In this article we covered how to answer of particular generic question
5 min read
NodeJS Interview Experience
Round - 1 (Technical - Basics)The first round consists of testing knowledge of JavaScript & database concepts.Initially, the interviewer asked me about the variable scoping concepts along with their coding examples.He asked me to differentiate between let, var, and const variables and tested my
3 min read
LTIMindTree Interview Experience For Java Developer
The interview questions you encounter in LTIMindTree Interview will primarily be influenced by the specific project for which you're being interviewed. Tell me about your IT experience and projects you worked on?What is the difference between method overloading and method overriding?What is a marker
1 min read
Accolite Digital Interview Experience for Senior Java Developer
I received the Technical recruiter's direct call for the role of Senior Java Developer - Backend. After initial discussion over the phone call. They scheduled the interview on Turbohire. There were 3 rounds: Technical - Video InterviewManagerial - Video InterviewHR - Telephonic discussion First-roun
2 min read
Infosys Interview Experience for Java Developer
My Infosys Interview started at sharp 4:00 pm, it was on the cisco webex platform. I got the interview opportunity through the Infosys portal after applying. My interview was for the Java Developer-Java role. The interview continues for one hour. The questions were based on the skills I mentioned on
2 min read
ForeScout Interview Experience For Java Developer
Tell me about your IT experience and projects you worked on.What is Lambda in AWS? What does it mean by serverless? Is it can run complex queries? What is SQS in AWS? What is the difference b/w a normal queue and a fifo in SQS? What is SNS and its uses in AWS? When to use which dB in AWS?Given a lis
2 min read
Accolite Digital Interview Experience for Java Developer | 4 Years Experienced
Round 1(Online Test): MCQ's Round 2: Design LRU cache and write the code and the operation complexity must be O(1)API Management tools How can u make your API's secureLogger management tools.Java basic questions Round 3: What are the design principals and design patterns?Talk about facade design pat
1 min read