← Home · Blog

JSON.stringify vs JSON.parse — Differences Explained

Published: July 25, 2025 · 6 min read

These two functions are the core of JSON handling in JavaScript. They work in opposite directions, and understanding them fully — including their quirks — will save you from hard-to-debug bugs.

The One-Line Summary

FunctionDirectionReturns
JSON.parse(str)JSON string → JS valueObject, array, string, number, boolean, or null
JSON.stringify(val)JS value → JSON stringString

JSON.parse() in Depth

Signature: JSON.parse(text, reviver?)

// Basic usage const obj = JSON.parse('{"name":"Alice","age":30}'); // → { name: "Alice", age: 30 } // Throws SyntaxError on invalid input JSON.parse("{bad json}"); // SyntaxError! // With reviver — transform values as they're parsed const data = JSON.parse('{"created":"2025-01-01T00:00:00Z","value":42}', (key, val) => { if (key === "created") return new Date(val); return val; }); console.log(data.created instanceof Date); // true

JSON.stringify() in Depth

Signature: JSON.stringify(value, replacer?, space?)

// Basic usage JSON.stringify({ a: 1, b: "hello" }); // '{"a":1,"b":"hello"}' // Pretty-print with indent JSON.stringify({ a: 1 }, null, 2); // '{ // "a": 1 // }' // With tab indentation JSON.stringify({ a: 1 }, null, "\t"); // Replacer as array — whitelist specific keys JSON.stringify({ name: "Alice", password: "secret", age: 30 }, ["name", "age"]); // '{"name":"Alice","age":30}' — password is excluded // Replacer as function — transform or filter values JSON.stringify({ name: "Alice", joined: new Date("2025-01-01") }, (key, val) => { if (val instanceof Date) return val.toISOString().split("T")[0]; // date only return val; }); // '{"name":"Alice","joined":"2025-01-01"}'

What JSON.stringify Silently Drops

This is the most common source of bugs. These JavaScript values are not representable in JSON:

const tricky = { fn: () => "hello", // → dropped (key removed) undef: undefined, // → dropped (key removed) sym: Symbol("x"), // → dropped (key removed) nan: NaN, // → null inf: Infinity, // → null negInf: -Infinity, // → null date: new Date(), // → ISO string (auto-converted) regex: /abc/, // → {} (empty object!) map: new Map([[1,2]]), // → {} (empty object!) }; console.log(JSON.stringify(tricky, null, 2)); // { // "nan": null, // "inf": null, // "negInf": null, // "date": "2025-07-25T10:00:00.000Z", // "regex": {}, // "map": {} // }

Key rule: If a value would serialize as undefined, it gets dropped entirely when it's an object property. If it's in an array, it becomes null.

JSON.stringify([1, undefined, 3]); // "[1,null,3]" JSON.stringify({ a: undefined }); // "{}"

toJSON() — Custom Serialization

You can control how any object is serialized by adding a toJSON() method. JSON.stringify calls it automatically.

class Money { constructor(amount, currency) { this.amount = amount; this.currency = currency; } toJSON() { return `${this.amount} ${this.currency}`; } } const price = new Money(29.99, "USD"); JSON.stringify({ price }); // '{"price":"29.99 USD"}'

Circular Reference Error

Trying to stringify an object that references itself throws a TypeError.

const obj = { name: "Alice" }; obj.self = obj; // circular reference! JSON.stringify(obj); // TypeError: Converting circular structure to JSON

Solutions: use a replacer function to detect and skip circular refs, or use a library like flatted or json-stringify-safe for serializing circular structures.

Quick Reference

Use CaseUse This
Parse API response bodyJSON.parse(text) or response.json()
Send data in fetch bodyJSON.stringify(data)
Save to localStoragelocalStorage.setItem(key, JSON.stringify(val))
Read from localStorageJSON.parse(localStorage.getItem(key))
Deep clone (simple objects)JSON.parse(JSON.stringify(obj))
Pretty-print for debuggingJSON.stringify(obj, null, 2)
Filter sensitive fieldsJSON.stringify(obj, ["field1","field2"])

Try Our JSON Tools

  • JSON Formatter — equivalent to JSON.stringify(data, null, 2) in your browser
  • Fix JSON — repair malformed JSON that causes JSON.parse() to throw
  • JSON Tree Viewer — explore any JSON structure visually

More Articles

How to Parse JSON in JavaScript

5 Common JSON Errors and How to Fix Them

How to Flatten Nested JSON

What is JSON — A Simple Explanation

← All Articles · Home · About · Privacy Policy