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
| Function | Direction | Returns |
|---|---|---|
JSON.parse(str) | JSON string → JS value | Object, array, string, number, boolean, or null |
JSON.stringify(val) | JS value → JSON string | String |
JSON.parse() in Depth
Signature: JSON.parse(text, reviver?)
JSON.stringify() in Depth
Signature: JSON.stringify(value, replacer?, space?)
What JSON.stringify Silently Drops
This is the most common source of bugs. These JavaScript values are not representable in JSON:
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.
toJSON() — Custom Serialization
You can control how any object is serialized by adding a toJSON() method. JSON.stringify calls it automatically.
Circular Reference Error
Trying to stringify an object that references itself throws a TypeError.
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 Case | Use This |
|---|---|
| Parse API response body | JSON.parse(text) or response.json() |
| Send data in fetch body | JSON.stringify(data) |
| Save to localStorage | localStorage.setItem(key, JSON.stringify(val)) |
| Read from localStorage | JSON.parse(localStorage.getItem(key)) |
| Deep clone (simple objects) | JSON.parse(JSON.stringify(obj)) |
| Pretty-print for debugging | JSON.stringify(obj, null, 2) |
| Filter sensitive fields | JSON.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