JSON
The universal format for sending data between apps — and the language your MERN front-end and back-end will speak.
What you will learn
- Recognise JSON
- Convert between objects and JSON
- Understand why JSON matters for APIs
JSON = data as text
JSON (JavaScript Object Notation) is a text format for data that looks almost exactly like a JavaScript object. It is how data travels across the internet between a front-end (the page running in the browser) and a server (the computer that stores and sends data) — usually through an API (a way for two programs to talk to each other and swap data).
{
"name": "Asha",
"age": 22,
"skills": ["HTML", "CSS", "JS"]
}This is what JSON looks like. Notice the small differences from a JS object: every key is wrapped in double quotes ("name", "age", "skills"), and the whole thing is really just text. That plain-text form is what makes it safe to send across the internet.
Convert both ways
JSON.stringify() turns an object into a JSON string (to send). JSON.parse() turns JSON text back into an object (to use).
<script>
const user = { name: "Asha", age: 22 };
const text = JSON.stringify(user); // object → JSON text
document.write(text + "<br>");
const back = JSON.parse(text); // JSON text → object
document.write("Name is " + back.name);
</script>The round-trip works in three steps:
- Start with a real JavaScript object
userthat you can use in code. JSON.stringify(user)flattens it into the text{"name":"Asha","age":22}— ready to send over the network or save.JSON.parse(text)rebuilds a usable object from that text, soback.namereads "Asha" again.
Note: Output: {"name":"Asha","age":22} Name is Asha
Tip: In MERN, your React app and your Node server pass data as JSON constantly. stringify when sending, parse when receiving — you will do this in every project.
Q. Which method turns a JavaScript object into a JSON string?
✍️ Practice
- Create an object,
stringifyit, and print the JSON text. - Take a JSON string and
parseit back into an object, then read a property.
🏠 Homework
- Write a note: why is JSON useful for sending data between a website and a server?