📦 JSON (JavaScript Object Notation)
Last Updated: 26th October 2025
JSON is a lightweight data format used to store and exchange data.
It is text-based and language-independent, but easily usable in JavaScript.
Hinglish Tip 🗣: JSON ko samjho ek "structured text format" jaise notebook me table bana ho aur usko easily share kar sake.
Syntax Rules
- Data is in key-value pairs.
- Keys must be strings inside double quotes
"key". - Values can be:
- String →
"value" - Number →
100 - Boolean →
true/false - Null →
null - Array →
[1, 2, 3] - Object →
{"key":"value"}
- String →
- No trailing commas allowed.
Example 1: Simple JSON Object
let person = {
name: "Amit",
age: 25,
isStudent: true,
hobbies: ["reading", "traveling", "coding"],
address: {
city: "Delhi",
zip: "110001",
},
};
console.log(person.name); // Amit
console.log(person.hobbies[1]); // traveling
console.log(person.address.city); // Delhi
Example 2: JSON Array
let users = [
{ name: "Amit", age: 25 },
{ name: "Suraj", age: 30 },
{ name: "Rahul", age: 35 },
];
console.log(users[1].name); // Suraj
console.log(users[2].age); // 35
Converting Between JSON and JavaScript Objects
- JSON.stringify()
Converts a JavaScript object/array into JSON string.
let user = { name: "Sadhu", age: 1 };
let json = JSON.stringify(user);
console.log(json); // {"name":"Sadhu","age":1}
- JSON.parse()
Converts a JSON string into a JavaScript object.
let json = '{"name":"Sadhu","age":1}';
let user = JSON.parse(json);
console.log(user); // {name: "Sadhu", age: 1}
Hinglish Tip 🗣: stringify() → JS object ko text banata hai, parse() → text ko JS object me badalta hai.
💡 Quick Practice
- Create a JSON object for a book with title, author, price, and genres (array).
- Convert it to JSON string and then parse it back to JS object.
- Access nested properties like genres[1] or author.