Learn JavaScript
❯ collections.json

JSON: parse & stringify in JavaScript

$concept

JSON is JavaScript's data interchange format with a strict subset syntax (double quotes, no functions/undefined/comments, no trailing commas). JSON.stringify converts values (dropping functions/undefined/symbols, throwing on circular refs) with replacer functions/arrays and space indentation for pretty output; JSON.parse reverses with optional reviver transforms (date revival being classic).

Structured cloning (structuredClone()) deep-copies richer values (Maps, Sets, Dates, circular!) where JSON round-trips fall short — but JSON remains the wire format for APIs and storage everywhere.

$how it works
1

stringify serializes

JSON.stringify({a: 1}) → '{"a":1}' — functions/undefined/symbols dropped silently, circular refs throw.

2

parse with validation

JSON.parse(text) throws SyntaxError on malformed input — always try/catch untrusted payloads.

3

Replacer/reviver transform

Replacer functions filter/alter during stringify; revivers (new Date(v)) restore types during parse.

4

Clone beyond JSON

structuredClone() deep-copies Maps, Dates, typed arrays, even circular graphs — where JSON cannot go.

$example
let user = { name: "Ada", tags: ["dev"] };
let text = JSON.stringify(user);
let back = JSON.parse(text);
console.log(text.length > 0, back.tags[0]);
Output
true dev
1stringify serializes the object to a JSON text wire format.
2parse rebuilds an equal-but-distinct object — back.tags[0] reads dev.
3Round trip complete — text in the middle, objects at both ends.
$use cases
API payloads serialized and parsed at every boundary
localStorage persistence via stringify/parse pairs
Revivers restoring Dates and classes from plain JSON
structuredClone for deep copies JSON cannot express

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.