JSON (JavaScript Object Notation) is a lightweight, text-based format for representing structured data. Despite the name, it's used far beyond JavaScript — it's the standard format for sending data between a browser and a server, for config files, and for storing structured data as plain text. JavaScript has two built-in methods that convert between real JavaScript objects and JSON text: JSON.stringify() and JSON.parse().

It's important to understand that JSON is always just a string — not a JavaScript object. You can't call .name on a JSON string directly; you first have to parse it back into an actual object.

Converting an Object to a JSON String — JSON.stringify()

const user = {
  name: "Simran",
  age: 24,
  skills: ["HTML", "CSS", "JavaScript"]
};

const jsonString = JSON.stringify(user);

console.log(jsonString);
console.log(typeof jsonString); // it's just a string now

Output:

{"name":"Simran","age":24,"skills":["HTML","CSS","JavaScript"]}
string

You can also pass a third argument to JSON.stringify(user, null, 2) to pretty-print it with 2-space indentation — useful when logging JSON for debugging.

Converting a JSON String Back to an Object — JSON.parse()

const jsonString = '{"name":"Simran","age":24}';

const userObject = JSON.parse(jsonString);

console.log(userObject.name);
console.log(typeof userObject); // back to a real object

Output:

Simran
object

If the string you pass to JSON.parse() isn't valid JSON — a trailing comma, single quotes instead of double quotes around keys, etc. — it throws a SyntaxError, so parsing data from an untrusted source (like a file or network response) is usually wrapped in a try...catch.

A Real Use Case: Saving to localStorage

localStorage can only store strings, so saving an object means stringifying it first and parsing it back when you read it:

const settings = { theme: "dark", fontSize: 16 };

// Save
localStorage.setItem("settings", JSON.stringify(settings));

// Load
const saved = JSON.parse(localStorage.getItem("settings"));
console.log(saved.theme);

Output:

dark

This same pattern is exactly how data coming back from a server gets turned into usable JavaScript objects — the response body arrives as JSON text, and response.json() parses it for you automatically.