SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
JavaScript
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10
Try this code live
Lesson 25 of 35

JavaScript JSON — stringify & parse with Examples

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.

Diagram — see how it fits together
Diagram: JavaScript JSON

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.

JSON.stringify — object to text

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.

JSON.parse — text back to object

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).

Note: JSON looks like a JavaScript object, but it is not one. Three rules: names must be in double quotes, values can only be strings, numbers, booleans, null, arrays or objects — no functions, no undefined, no comments, and no trailing comma after the last item. If you break a rule, JSON.parse() fails.

Where you meet JSON every day

  • Fetching data — when a page loads a list of courses or exam results from a server, the answer is almost always JSON.
  • Browser storage — localStorage can only store strings, so you stringify an object before saving it and parse it when you read it back.
  • Forms and APIs — when a form is sent to a server, the data is usually packed into JSON first.
Tip: Remember it like this: JSON is a letter, not a parcel — text that describes the object. stringify writes the letter, parse opens it. If you remember which way each one goes, you will never mix them up.

Common mistakes

  • Using single quotes in JSON — {'name': 'Priya'} is valid JavaScript but broken JSON. JSON needs double quotes.
  • Leaving a trailing comma — {"a": 1,} fails to parse. The last item has no comma after it.
  • Putting a function in JSON — functions are dropped silently by stringify. JSON carries data, not behaviour.
  • Forgetting that parse throws on bad text — wrap it in try...catch when the text comes from outside your code.
  • Using the parsed result as a string — after parse you have a real object, so use back.name, not back["name"] as text.

Remember

  • JSON is text that looks like an object — it is how data travels between computers.
  • JSON.stringify(object) turns an object into a JSON string.
  • JSON.parse(text) turns a JSON string back into an object.
  • JSON rules: double quotes only, no functions, no trailing commas.
  • JSON is a letter, not a parcel — text that describes the object.

Example explained

  • 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.

Example

Example
<!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>

Output

Output — how it looks in the browser
Output of JavaScript JSON — stringify & parse with Examples

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is JSON and how is it different from an object?

JSON is text that looks like an object so it can travel between computers; an object holds real values you use directly.

2. How do I turn an object into JSON text?

With JSON.stringify, which always uses double quotes around names and string values.

3. What happens if JSON.parse gets broken text?

It throws an error, so it is a good idea to wrap it in try...catch.

Why learn this yourself — and not just ask AI?

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.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
View
Summer Industrial Training in MERN Stack 2026
₹3,000
View
AI & ML with Azure AI, AWS AI & Generative AI
₹10,000
View
Browse all courses
JavaScript — Lessons
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →