JSON
A simple, text-based format for representing data, used constantly to send information between programs.
What is it?
When two programs share data (say, a browser and a server), they need a common text format, because JavaScript objects only exist inside a running JavaScript program. JSON (JavaScript Object Notation) is that format: plain text that looks like a JavaScript object or array, and that almost every language can read and write.
Explain like I'm 10
JSON is like a standard recipe card format. Any chef (any language) can read a card in that format, even if their kitchen is completely different from the one that wrote it.
Examples
Converting between objects and JSON text
const user = { name: "Amara", age: 28 };
const json = JSON.stringify(user);
console.log(json); // '{"name":"Amara","age":28}' — now just text
const parsed = JSON.parse(json);
console.log(parsed.name); // "Amara" — back to a real objectSaving and restoring data with localStorage
const settings = { theme: "dark", fontSize: 16 };
localStorage.setItem("settings", JSON.stringify(settings));
// ...later, maybe after a page reload:
const saved = JSON.parse(localStorage.getItem("settings"));
console.log(saved.theme); // "dark"localStorage only stores strings, so JSON is the standard way to save an object there and get a real object back later.
How it works
JSON.stringify walks an object or array and produces text that follows JSON's strict rules (double-quoted keys, no functions, no undefined). JSON.parse does the reverse and rebuilds the JavaScript value from the text. Both are pure text transformations. JSON's syntax was inspired by JavaScript object literals, but the format itself isn't tied to JavaScript.
Why does it exist?
Before JSON, systems exchanging structured data (especially over the web) often used wordier formats like XML, or made up their own. JSON's simplicity and likeness to JavaScript objects made it the main format for APIs and configuration.
When to use it
Use JSON to send structured data between client and server (most API responses are JSON), to save structured data to a file or localStorage, or to pass data between programs in different languages.
When not to use it
JSON can't represent every JavaScript value. Functions and undefined are silently dropped, and circular references cause an error. For data that needs those, or that's very large and performance-sensitive, another format may fit better.
Common mistakes
Trying to
JSON.stringifyan object containing functions orundefinedvalues and being surprised they're silently dropped.Forgetting that
JSON.parsethrows on invalid JSON, and not wrapping it in try/catch when the input might be invalid.Assuming JSON allows comments or trailing commas. It doesn't; it's stricter than a JavaScript object literal.
Practice exercises
- Easy:
Convert a small object to a JSON string with
JSON.stringify, then back withJSON.parse, and confirm the values match. - Medium:
Store an object in
localStorageas a JSON string, then read and parse it back on page load. - Hard:
Write a function that safely parses possibly-invalid JSON text, returning
nullinstead of throwing if it's malformed.
Interview questions
What does `JSON.stringify` do?
Converts a JavaScript value into a JSON-formatted text string.
What happens to functions and `undefined` values when you stringify an object containing them?
They're left out of the JSON string entirely. JSON has no way to represent them.
Why is JSON so commonly used for APIs?
It's simple, lightweight, human-readable, and supported natively or via libraries in virtually every programming language.