JSON

JSON (JavaScript Object Notation) is a plain-text way of writing structured data. It looks almost exactly like a JavaScript object or array, but it's actually just a string — which is what makes it useful for sending data between places that don't share memory, like a browser and a server.

JSON is text, not objects

The value below looks like an object, but it's written between single quotes — it's a string that happens to contain JSON-formatted text:

Try it yourself
Console output

JSON looks similar to a JavaScript object literal, but it's stricter: keys must be wrapped in double quotes, trailing commas aren't allowed, and there's no way to represent a function.

JSON.stringify — turn a value into JSON text

JSON.stringify converts a real JavaScript object or array into its JSON text form, ready to be sent somewhere or saved:

Try it yourself
Console output

JSON.parse — turn JSON text back into a value

JSON.parse does the reverse: it reads a JSON string and gives you back a real, usable object:

Try it yourself
Console output

Why it's used for data exchange

A browser and a server don't share JavaScript memory — the only thing that actually travels over a network connection is text. So the sending side calls JSON.stringify to turn its data into text, and the receiving side calls JSON.parse to turn that text back into an object it can work with. The same trick applies to saving data in localStorage, which only ever stores strings:

Try it yourself
Console output
Note: JSON.stringify silently drops anything it can't represent as JSON, including functions and undefined values — they just vanish from the output instead of causing an error. Try adding a function property to an object below and see what happens:
Try it yourself
Console output