4 min read · Free · No login needed
Websites rarely work alone. Your page talks to a server: it asks for a student's record, a list of courses, or a chai order — and the server answers. That answer travels as text, because text is the one thing every computer can read. The format of that text is called JSON — JavaScript Object Notation. It looks like a JavaScript object, but it is only text.
Think of it like a letter. A JavaScript object is a parcel — it holds real things you can use directly. JSON is the letter that describes the parcel, so it can travel by post. When the letter arrives, you open it and rebuild the parcel. JSON.stringify() writes the letter. JSON.parse() opens it.
let student = { name: "Priya", course: "HTML", marks: 92 };
let text = JSON.stringify(student);
// text is '{"name":"Priya","course":"HTML","marks":92}'
The result is a plain string. You can send it to a server, save it in the browser's storage, or print it on the page. Notice the double quotes — JSON always uses double quotes around names and string values. Single quotes are not allowed in JSON.
let back = JSON.parse(text); console.log(back.name); // "Priya"
JSON.parse() reads the text and rebuilds the object, so you can use back.name and back.marks like normal. If the text is broken — a missing quote, a trailing comma — parse throws an error, so it is a good idea to wrap it in try...catch (the lesson before this one).
undefined, no comments, and no trailing comma after the last item. If you break a rule, JSON.parse() fails.localStorage can only store strings, so you stringify an object before saving it and parse it when you read it back.stringify writes the letter, parse opens it. If you remember which way each one goes, you will never mix them up.{'name': 'Priya'} is valid JavaScript but broken JSON. JSON needs double quotes.{"a": 1,} fails to parse. The last item has no comma after it.stringify. JSON carries data, not behaviour.parse throws on bad text — wrap it in try...catch when the text comes from outside your code.parse you have a real object, so use back.name, not back["name"] as text.JSON.stringify(object) turns an object into a JSON string.JSON.parse(text) turns a JSON string back into an object.let student = { name: "Rahul", course: "CSS", marks: 88 }; — a normal JavaScript object, the parcel.let text = JSON.stringify(student); — writes the letter: a string with double quotes around every name and string value.document.getElementById("jsonText").textContent = text; — the JSON text is shown in the box on the page, so you can see exactly what travels to the server.let back = JSON.parse(text); — opens the letter again and rebuilds the object.document.getElementById("out").textContent = back.name + " scored " + back.marks; — the rebuilt object works like the original: back.name is "Rahul" and back.marks is 88.<!DOCTYPE html>
<html>
<head>
<title>Student Card — JSON</title>
<style>
body { font-family: Verdana, sans-serif; background-color: #fff8e1; }
h1 { color: #b45309; text-align: center; }
.card { background-color: white; border: 2px solid #f59e0b; padding: 16px; width: 380px; margin: 20px auto; }
button { background-color: #b45309; color: white; border: none; padding: 10px 18px; font-size: 16px; }
pre { background-color: #f1f5f9; padding: 10px; }
#out { font-weight: bold; color: green; }
</style>
</head>
<body>
<h1>Student Card</h1>
<div class="card">
<p><button onclick="sendCard()">Send my card as JSON</button></p>
<p>The JSON text that travels:</p>
<pre id="jsonText">{"name":"Rahul","course":"CSS","marks":88}</pre>
<p id="out">Click the button to pack the card, send it, and open it again.</p>
</div>
<script>
function sendCard() {
let student = { name: "Rahul", course: "CSS", marks: 88 };
let text = JSON.stringify(student);
document.getElementById("jsonText").textContent = text;
let back = JSON.parse(text);
document.getElementById("out").textContent = back.name + " scored " + back.marks + " in " + back.course + ".";
}
</script>
</body>
</html>
Change the code below and press Run to see the result instantly. No file to save, no login needed.
AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.
Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.
Take the testPrefer watching? Video courses teach the same topics step by step.
Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.
Browse all coursesAsk about courses, website & more
Request human callback:
Download karne ke liye login karo. Login ke baad yahi page pe rahoge!
Naya ho? Register karo →