Arrays & ObjectsExtra· 30 min read

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

JSON: keys in double quotes
{
  "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).

stringify to send, parse 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>
Live preview

The round-trip works in three steps:

  1. Start with a real JavaScript object user that you can use in code.
  2. JSON.stringify(user) flattens it into the text {"name":"Asha","age":22} — ready to send over the network or save.
  3. JSON.parse(text) rebuilds a usable object from that text, so back.name reads "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?

Answer: JSON.stringify() serialises an object to a JSON string (for sending). JSON.parse() does the reverse.

✍️ Practice

  1. Create an object, stringify it, and print the JSON text.
  2. Take a JSON string and parse it back into an object, then read a property.

🏠 Homework

  1. Write a note: why is JSON useful for sending data between a website and a server?
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →