JSON.stringify() vs JSON.parse(): The Complete Comparison
JSON.stringify() and JSON.parse() are inverse operations that work together to enable JSON-based data persistence, transfer, and communication. Here is everything about both.
What Each Function Does
JSON.stringify() serializes: it takes a JavaScript value and returns a JSON string. JSON.parse() deserializes: it takes a JSON string and returns a JavaScript value. They are inverses — JSON.parse(JSON.stringify(obj)) produces a deep copy of obj (with the caveat that non-serializable values are lost).
Serialization is used when you need to store or transmit data: writing to localStorage, sending an HTTP request body, writing to a file. Deserialization is used when you receive or read that data: reading from localStorage, parsing an HTTP response, reading a file.
Type Transformations During the Round-Trip
Not all JavaScript values survive a stringify/parse round-trip unchanged. Date objects become strings (JSON.stringify converts them via toISOString()). undefined values and functions are omitted. NaN and Infinity become null. These transformations can cause subtle bugs if you do not account for them.
The safest rule is to only stringify/parse data composed of the six JSON types: strings, numbers, booleans, null, arrays, and plain objects. For any other type — Date, Map, Set, class instances — implement explicit conversion logic rather than relying on stringify/parse to handle it.
Performance: Which Is Faster?
JSON.parse() is generally faster than JSON.stringify() for equivalent data. Parsing is a simpler operation (read tokens and build a structure) than serialization (traverse an object graph, handle all edge cases, escape strings). For large objects, parsing is typically 2-3x faster than stringifying.
Both operations are highly optimized in modern JavaScript engines. V8, SpiderMonkey, and JavaScriptCore all treat JSON.parse() as a special case and use native C++ implementations. For typical workloads, neither function is a performance concern.
Common Patterns Using Both Together
Deep cloning: const clone = JSON.parse(JSON.stringify(obj)) creates a deep copy of a plain object. This works reliably for JSON-compatible objects but drops functions, undefined values, and Date objects (which become strings). For objects with these types, use structuredClone() instead.
localStorage persistence: localStorage.setItem("state", JSON.stringify(appState)) followed by JSON.parse(localStorage.getItem("state") ?? "null") is the standard pattern for persisting and restoring state across page loads.
Try JSON Stringify Online Free Online
No sign-up required. 100% client-side — your data never leaves your browser.
Open JSON Stringify Onlinearrow_forwardFrequently Asked Questions
Can JSON.parse(JSON.stringify(obj)) deep clone a Date?
No. Dates become ISO strings after the round-trip. Use structuredClone(obj) for deep cloning that preserves Date objects, Maps, Sets, and other types that JSON does not support.
Why does JSON.stringify(undefined) return undefined (not a string)?
undefined is not a valid JSON value, so JSON.stringify returns undefined (the JavaScript value, not a string) rather than throwing. This is consistent with how undefined is handled everywhere in JSON serialization.
Is there a way to make JSON.stringify handle circular references?
Not natively — JSON.stringify throws TypeError for circular references. Use a library like flatted or serialize-javascript that extends JSON serialization to handle circular structures.