Understanding JSON (JavaScript Object Notation) is fundamental for working with
localStorage, APIs, databases, and more in modern web development. Let’s break it down
clearly and deeply:
✅ What is JSON?
JSON stands for JavaScript Object Notation.
It is a lightweight data format used to store and exchange data. It looks like JavaScript
objects, but is strictly a string-based format.
🔹 Example of JSON:
{
"name": "M Sabid",
"age": 25,
"skills": ["JavaScript", "React", "[Link]"],
"isMentor": true
}
It’s:
● Human-readable ✅
● Easy for machines to parse ✅
● Used everywhere: APIs, configs, databases, localStorage ✅
🧠 Why JSON?
Reason Explanation
Standard format Almost every programming language supports JSON (Python,
PHP, Java, etc.)
Lightweight Smaller and simpler than XML
Language-independe Though inspired by JavaScript, it works everywhere
nt
Perfect for APIs Most APIs (REST, GraphQL) use JSON to send and receive
data
Perfect for storage Use in localStorage, databases (e.g., MongoDB), configs
🔧 How to Use JSON in JavaScript?
1. ✅ Create a JavaScript Object
const user = {
name: "M Sabid",
age: 25,
isLoggedIn: true
};
2. 🔄 Convert (Stringify) JavaScript Object to JSON
You can’t store objects directly in localStorage — you need to convert it into a string.
const jsonString = [Link](user);
[Link](jsonString);
// Output: {"name":"M Sabid","age":25,"isLoggedIn":true}
3. 🔁 Parse JSON string back to JavaScript Object
When you retrieve it, you get a string — so you need to convert it back into an object.
const parsedUser = [Link](jsonString);
[Link]([Link]); // "M Sabid"
🛑 JSON Rules (Strict Format)
Rule Example
✅ Use double quotes for keys and strings "name": "Sabid"
❌ No comments allowed // this is invalid
in JSON
✅ Only valid data types: string, number, object, array, "age": 23, "skills":
boolean, null ["HTML"]
📦 JSON with localStorage (Mini Recap)
// Save object
[Link]("user", [Link](user));
// Get and convert back
const userFromStorage = [Link]([Link]("user"));
🧪 Bonus Tip: JSON vs Object
Feature JSON JavaScript Object
Format Always string Native JS format
Used for Data transfer, storage Logic, manipulation
Can contain ❌ No functions allowed ✅ Can contain
functions functions
Conversion Needs [Link]() and Directly usable
[Link]()