JSON

Parsing and serialising JSON in JavaScript — JSON.parse, JSON.stringify, replacers, revivers, and common pitfalls.

What it is, in plain English: JSON (JavaScript Object Notation) is a text format for storing and transferring data. It looks like JavaScript object literal syntax but is stricter: keys must be quoted strings, and only six value types are allowed (string, number, boolean, null, array, object). In JavaScript, JSON.stringify() converts a value to a JSON string, and JSON.parse() converts a JSON string back to a JavaScript value.

Storing and transferring data as text

When your JavaScript app needs to save data to a file, send it to a server, or store it in localStorage, it has to convert the data to a string first. JSON is the universal format for that — every language and platform understands it.

JSON.stringify() — convert a value to a JSON string

const product = {
  name:  "Wireless Headphones",
  price: 2999,
  inStock: true,
  tags: ["audio", "wireless"],
};

const json = JSON.stringify(product);
console.log(json);
// {"name":"Wireless Headphones","price":2999,"inStock":true,"tags":["audio","wireless"]}
console.log(typeof json);  // "string"

// Pretty-print (easier to read):
console.log(JSON.stringify(product, null, 2));
// {
//   "name": "Wireless Headphones",
//   "price": 2999,
//   ...
// }

JSON.parse() — convert a JSON string back to a value

const text = '{"name":"Alice","age":30,"scores":[95,87,72]}';

const obj = JSON.parse(text);
console.log(obj.name);     // "Alice"
console.log(obj.age);      // 30
console.log(obj.scores);   // [95, 87, 72]

// Common use: read from localStorage
localStorage.setItem("user", JSON.stringify({ name: "Bob" }));
const saved = JSON.parse(localStorage.getItem("user"));
console.log(saved.name);  // "Bob"

What JSON supports — and what it drops

JSON is not JavaScript. JSON only supports 6 types: string, number, boolean, null, array, and object (with string keys). Everything else is silently dropped or converted when you stringify.
const data = {
  name:    "Carol",             // ✓ string → kept
  age:     25,                  // ✓ number → kept
  active:  true,                // ✓ boolean → kept
  address: null,                // ✓ null → kept
  greet:   function() {},       // ✗ function → DROPPED
  id:      undefined,           // ✗ undefined → DROPPED
  sym:     Symbol("x"),         // ✗ Symbol → DROPPED
  created: new Date("2026-01-01"), // Date → converted to string
};

console.log(JSON.stringify(data));
// {"name":"Carol","age":25,"active":true,"address":null,"created":"2026-01-01T00:00:00.000Z"}

The deep copy trick (and its limits)

// JSON.parse(JSON.stringify(obj)) creates a deep copy:
const original = { a: 1, b: { c: 2 } };
const copy = JSON.parse(JSON.stringify(original));
copy.b.c = 99;
console.log(original.b.c);  // 2 — original unchanged!

// But this fails for: functions, undefined, Dates (become strings),
// Sets, Maps, RegExp, Infinity, NaN (become null).
// For a real deep copy use: structuredClone(obj)
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.