โ† Home ยท Blog

JSON.stringify vs JSON.parse โ€” Differences Explained

Published: July 25, 2025 ยท Last updated: September 7, 2026 ยท 6 min read ยท By JSON Suite Team

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. For a deeper dive into JSON parsing, see our complete JavaScript JSON parsing guide.

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"])

Learn More

For complete coverage of JSON in JavaScript, check our comprehensive JavaScript JSON guide. Watch out for common JSON errors when parsing external data. If you're working with Python, see our Python JSON parsing guide. For fundamentals, start with what is JSON.

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

Related Tools

Work with JSON.stringify and JSON.parse using these tools:

More Articles

How to Parse JSON in JavaScript

10+ Common JSON Errors and How to Fix Them

How to Flatten Nested JSON

What is JSON โ€” A Complete Guide