JSON is one of the most commonly used data formats in modern web development. It is widely used for REST APIs, AJAX requests, web services, configuration files, frontend-backend communication, and data exchange.
For PHP, JavaScript, Node.js, Python, Java, and other backend/frontend technologies, understanding JSON is essential.
Below are 25 important JSON interview questions and answers, ranging from basic concepts to practical and advanced interview questions.
1. What is JSON?
Answer:
JSON stands for JavaScript Object Notation. It is a lightweight, text-based format used to store and exchange structured data.
Example:
{
"name": "Rahul",
"age": 30,
"city": "Delhi"
}
JSON is language-independent, although its syntax was inspired by JavaScript object notation.
2. Why is JSON used?
JSON is mainly used for:
API communication
Data exchange between frontend and backend
RESTful web services
AJAX requests
Configuration files
Storing structured data
Communication between different programming languages
For example, a PHP backend can return JSON data to a JavaScript frontend.
3. What are the basic data types supported by JSON?
JSON supports six main data types:
String
Number
Boolean
Object
Array
Null
Example:
{
"name": "Amit",
"age": 25,
"active": true,
"skills": ["PHP", "MySQL"],
"address": {
"city": "Noida"
},
"middle_name": null
}
4. What is the difference between JSON and a JavaScript Object?
A JSON document is text/data representation, while a JavaScript object is an in-memory JavaScript value.
JSON:
{
"name": "Amit"
}
JavaScript object:
const user = {
name: "Amit"
};
JSON is commonly transferred over HTTP, while JavaScript objects are used directly by JavaScript code.
5. How do you convert a JavaScript object into JSON?
Use:
JSON.stringify()
Example:
const user = {
name: "Amit",
age: 25
};
const jsonData = JSON.stringify(user);
console.log(jsonData);
Output:
{"name":"Amit","age":25}
6. How do you convert JSON into a JavaScript object?
Use:
JSON.parse()
Example:
const jsonData = '{"name":"Amit","age":25}';
const user = JSON.parse(jsonData);
console.log(user.name);
Output:
Amit
7. What is JSON.stringify()?
JSON.stringify() converts a JavaScript value into a JSON string.
Example:
const data = {
id: 101,
name: "Rahul"
};
const json = JSON.stringify(data);
console.log(json);
It is commonly used before sending data to an API.
8. What is JSON.parse()?
JSON.parse() converts a valid JSON string into a JavaScript value.
Example:
const json = '{"id":101,"name":"Rahul"}';
const data = JSON.parse(json);
console.log(data.id);
9. What is a JSON object?
A JSON object is a collection of key-value pairs enclosed in {}.
Example:
{
"id": 101,
"name": "Rahul",
"email": "rahul@example.com"
}
Keys must be enclosed in double quotes.
10. What is a JSON array?
A JSON array is an ordered collection of values enclosed in [].
Example:
[
"PHP",
"MySQL",
"JavaScript"
]
An array can also contain objects:
[
{
"id": 1,
"name": "Rahul"
},
{
"id": 2,
"name": "Amit"
}
]
11. Can JSON contain nested objects?
Yes.
Example:
{
"name": "Rahul",
"address": {
"city": "Noida",
"state": "Uttar Pradesh",
"country": "India"
}
}
Here, address is a nested JSON object.
12. Can JSON contain arrays of objects?
Yes.
Example:
{
"students": [
{
"id": 1,
"name": "Rahul"
},
{
"id": 2,
"name": "Amit"
}
]
}
This structure is extremely common in REST APIs.
13. What is the difference between JSON and XML?
| JSON | XML |
|---|---|
| Lightweight | More verbose |
| Easy to read | More markup-heavy |
| Uses key-value structure | Uses tags |
| Supports arrays naturally | Arrays require structural conventions |
| Common in REST APIs | Common in legacy/enterprise integrations |
| Easy to work with JavaScript | Requires XML parsing |
Example JSON:
{
"name": "Rahul",
"age": 25
}
Equivalent XML:
Rahul
25
14. Are JSON keys case-sensitive?
Yes. JSON member names are strings, and applications commonly treat them as case-sensitive.
For example:
{
"Name": "Rahul",
"name": "Amit"
}
Name and name are different member names.
15. Can JSON have duplicate keys?
JSON syntax permits member names as strings, but duplicate names are not recommended because different parsers or implementations may handle duplicates differently.
Avoid:
{
"name": "Rahul",
"name": "Amit"
}
Prefer unique keys:
{
"first_name": "Rahul",
"last_name": "Kumar"
}
16. What is a valid JSON string?
A JSON string must use double quotes.
Valid:
{
"name": "Rahul"
}
Invalid:
{
'name': 'Rahul'
}
Single quotes are commonly used in JavaScript source code, but they are not valid JSON string delimiters.
17. What is JSON MIME type?
The standard media type for JSON is:
application/json
For example, an HTTP API response may contain:
Content-Type: application/json
This tells the client that the response body contains JSON.
18. How is JSON sent through an API?
A client can send JSON in an HTTP request body.
Example:
POST /api/users
Content-Type: application/json
Request body:
{
"name": "Rahul",
"email": "rahul@example.com"
}
The server parses the JSON and processes the submitted data.
19. How do you return JSON from PHP?
In PHP, use json_encode().
Example:
$data = [
"id" => 101,
"name" => "Rahul"
];
header('Content-Type: application/json');
echo json_encode($data);
Output:
{
"id": 101,
"name": "Rahul"
}
20. How do you decode JSON in PHP?
Use:
json_decode()
Example:
$json = '{"name":"Rahul","age":25}';
$data = json_decode($json, true);
echo $data['name'];
Output:
Rahul
The second argument true tells PHP to return associative arrays instead of objects.
21. What is the difference between json_decode() with and without true?
Without true:
$data = json_decode($json);
echo $data->name;
With true:
$data = json_decode($json, true);
echo $data['name'];
So:
json_decode($json);
returns a PHP object by default, while:
json_decode($json, true);
returns an associative array.
22. How do you handle invalid JSON in PHP?
You can use JSON_THROW_ON_ERROR:
try {
$data = json_decode(
$json,
true,
512,
JSON_THROW_ON_ERROR
);
} catch (JsonException $e) {
echo "Invalid JSON";
}
This is preferable to silently ignoring malformed input when robust error handling is required.
23. What is the difference between JSON and JSONP?
JSON is a data format.
JSONP (JSON with Padding) is an older technique for requesting data across origins using a [removed] element.
JSON:
{
"name": "Rahul"
}
JSONP:
callback({
"name": "Rahul"
});
JSONP has significant security and architectural limitations and is generally not preferred for modern APIs. CORS is the standard approach for cross-origin browser requests.
24. How can JSON data be validated?
JSON syntax can be validated before processing.
For example, in PHP:
try {
$data = json_decode(
$json,
true,
512,
JSON_THROW_ON_ERROR
);
echo "Valid JSON";
} catch (JsonException $e) {
echo "Invalid JSON";
}
For API systems, validation should include both:
JSON syntax validation
Business/data validation
For example, checking whether email exists and whether it has a valid format.
25. What are some best practices for using JSON in REST APIs?
Important best practices include:
1. Use the correct Content-Type
Content-Type: application/json
2. Use meaningful property names
Good:
{
"user_id": 101,
"first_name": "Rahul"
}
3. Maintain consistent structure
Avoid returning completely different structures for similar API responses.
4. Validate incoming JSON
Never blindly trust client-provided data.
5. Handle errors properly
Example:
{
"success": false,
"message": "Invalid email address"
}
6. Avoid unnecessarily deep nesting
Keep API responses easy to consume.
7. Don't expose sensitive information
Never return passwords, private tokens, or other sensitive data unnecessarily.
Bonus: JSON Interview Questions You Should Also Prepare
For experienced developers, also prepare these topics:
JSON Schema
JSON vs JSONB
JSON in MySQL
JSON indexes
JSON path expressions
REST API JSON responses
API pagination
JSON validation
JSON security
CORS
JSON Web Token (JWT)
JSON serialization/deserialization
Large JSON payload optimization
JSON normalization
JSON Merge Patch
JSON Patch
Quick JSON Revision
| Concept | Key Point |
|---|---|
| JSON | Data interchange format |
| Object | {} |
| Array | [] |
| String | Double quotes |
| Boolean | true / false |
| Null | null |
| JS Object → JSON | JSON.stringify() |
| JSON → JS Object | JSON.parse() |
| PHP Array → JSON | json_encode() |
| JSON → PHP | json_decode() |
| JSON MIME Type | application/json |
| REST APIs | Commonly use JSON |
| Cross-Origin Requests | CORS |
| JSONP | Older technique |
Interview Tip
For experienced-level interviews, don't stop at definitions. Be prepared to write JSON, parse JSON, validate API requests, handle malformed JSON, work with nested arrays/objects, and integrate JSON with PHP/MySQL/JavaScript APIs.
The most important functions to remember are:
JSON.stringify()
JSON.parse()
json_encode()
json_decode()
Master these 25 questions and you'll have a strong foundation for JSON-related technical interviews.