JavaScript JSON — Interview Questions & Answers
Ques: What is JSON in JavaScript?
Ans: JSON (JavaScript Object Notation) is a lightweight data interchange format used to store and exchange data between a client and a server.
It is language-independent but based on JavaScript object syntax.
Example:
{
"name": "Alice",
"age": 25,
"city": "Delhi"
}
Ques: What does JSON stand for?
Ans: JSON = JavaScript Object Notation
Ques: Why is JSON used?
- To exchange data between client and server.
- It’s easy to read and write.
- Supported by all major programming languages.
- Ideal for AJAX and APIs (e.g., REST APIs).
Ques: What is the syntax of JSON?
Ans: JSON is written as key-value pairs enclosed in {}.
Rules:
- Keys must be in double quotes
" ". - Data is separated by commas.
- Supports objects
{}and arrays[].
Example:
{
"employee": {
"name": "John",
"age": 30,
"skills": ["HTML", "CSS", "JS"]
}
}
Ques: What are the data types supported in JSON?
- String
- Number
- Boolean
- Null
- Object
- Array
Example:
{
"name": "Ravi",
"age": 22,
"isStudent": true,
"courses": ["Math", "Science"]
}
Ques: What are invalid JSON data types?
ndefinedfunction()Dateobjects (must be strings)- Comments
These must be converted before JSON parsing.
Ques: What is the difference between JSON and JavaScript objects?
| Feature | JSON | JavaScript Object |
|---|---|---|
| Syntax | Keys in double quotes | Keys can be without quotes |
| Type | Data format | Data structure |
| Methods | parse(), stringify() | Normal JS methods |
| Example | {"name": "John"} | {name: "John"} |
Ques: How do you convert JSON to a JavaScript object?
Ans: Use the JSON.parse() method.
Example:
let jsonData = '{"name":"John","age":25}';
let obj = JSON.parse(jsonData);
console.log(obj.name); // Output: John
Ques: How do you convert a JavaScript object to JSON?
Ans: Use the JSON.stringify() method.
Example:
let person = { name: "John", age: 25 };
let jsonText = JSON.stringify(person);
console.log(jsonText); // Output: {"name":"John","age":25}
Ques: What is JSON.parse() used for?
Ans: JSON.parse() converts a JSON string into a JavaScript object.
Example:
let obj = JSON.parse('{"x":10, "y":20}');
console.log(obj.x + obj.y); // 30
Ques: What is JSON.stringify() used for?
Ans: JSON.stringify() converts a JavaScript object or array into a JSON string.
Example:
let student = { name: "Alice", marks: 90 };
let jsonString = JSON.stringify(student);
console.log(jsonString);
Ques: What happens if JSON syntax is incorrect during parsing?
Ans: It throws a SyntaxError exception.
Always wrap JSON.parse() in a try...catch block.
Example:
try {
JSON.parse("{name:'John'}"); // invalid JSON
} catch (e) {
console.error("Invalid JSON:", e);
}
Ques: How do you handle circular references when stringifying?
Ans: JSON does not support circular structures.
Use libraries like flatted or manually remove circular references.
Ques: Can JSON contain functions or undefined values?
Ans: No, JSON supports only data — not methods, functions, or undefined.
Ques: How do you format (pretty print) a JSON string?
Ans: Pass extra parameters to JSON.stringify():
let obj = { name: "Bob", age: 20 };
console.log(JSON.stringify(obj, null, 2)); // Here 2 adds indentation
Ques: How do you loop through JSON data in JavaScript?
Ans: Convert it to an object first, then loop:
let json = '{"name":"Sam","age":23,"city":"Delhi"}';
let obj = JSON.parse(json);
for (let key in obj) {
console.log(key + ": " + obj[key]);
}
Ques: How is JSON used with AJAX?
Ans: AJAX requests often send/receive JSON data.
Example:
fetch("data.json")
.then(res => res.json())
.then(data => console.log(data));
Ques: How do you send JSON data to a server (POST)?
fetch("server.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "John", age: 30 })
})
.then(res => res.json())
.then(data => console.log(data));
Ques: How do you read JSON data from a server (GET)?
fetch("data.json")
.then(response => response.json())
.then(data => console.log(data));
Ques: How is JSON handled in PHP?
Ans: PHP provides built-in methods:
$json = '{"name":"John","age":25}';
$obj = json_decode($json);
echo $obj->name; // John
Convert PHP arrays to JSON:
echo json_encode(["name" => "John", "age" => 25]);
Ques: How do you display JSON data in HTML?
<div id="result"></div>
<script>
fetch("user.json")
.then(res => res.json())
.then(data => {
document.getElementById("result").innerHTML = data.name;
});
</script>
Ques: What’s the difference between JSON.parse() and eval()?
| Method | Description |
|---|---|
JSON.parse() | Safe and fast; only parses valid JSON |
eval() | Executes any JS code (unsafe and insecure) |
Ques: What are nested JSON objects?
Ans: When JSON objects contain other objects or arrays as values.
Example:
{
"user": {
"name": "Tom",
"skills": ["HTML", "CSS", "JS"]
}
}
Access in JS:
data.user.skills[1]; // CSS
Ques: How can you convert an array to JSON?
let arr = ["apple", "banana", "mango"];
let json = JSON.stringify(arr);
console.log(json); // ["apple","banana","mango"]
Ques: How do you convert JSON back to an array?
let json = '["red","green","blue"]';
let arr = JSON.parse(json);
console.log(arr[1]); // green
Ques: How do you validate if a string is valid JSON?
function isValidJSON(str) {
try {
JSON.parse(str);
return true;
} catch {
return false;
}
}
Ques: How do you merge multiple JSON objects?
let obj1 = { a: 1 }, obj2 = { b: 2 };
let merged = { ...obj1, ...obj2 };
console.log(merged); // { a: 1, b: 2 }
Ques: What is the MIME type of JSON?
Ans: application/json
Used in HTTP headers when sending JSON data:
headers: { "Content-Type": "application/json" }
Ques: What are common JSON file extensions?
Ans: .json → Used for configuration files, datasets, APIs.
Examples:
package.json(Node.js)db.json(mock data)
Ques: What are common JSON use cases?
- RESTful APIs
- Configuration files
- Data storage
- Web application data exchange
- NoSQL databases (e.g., MongoDB)
