JSON.parse() and JSON.stringify()
Convert between a JavaScript value and its JSON text representation — the standard way data crosses storage and network boundaries.
JSON.stringify(value) / JSON.parse(text)
Purpose
JSON.stringify converts a JavaScript value into a JSON-formatted string. JSON.parse converts a
JSON string back into a JavaScript value. Both are how data crosses a text-only boundary —
localStorage, a network request body, a file on disk.
Syntax
const data = { name: 'Ada', active: true };
const json = JSON.stringify(data); // '{"name":"Ada","active":true}'
const restored = JSON.parse(json); // a new object, equal in content, not identity
Behavior notes
JSON.stringifydrops any property whose value isundefined, a function, or aSymbolfrom an object entirely; the same values becomenullinside an array instead of being dropped.NaNandInfinityboth stringify tonull— JSON has no representation for either.Dateobjects stringify to an ISO string, butJSON.parsenever converts that string back into aDate— it comes back as a plain string, and the caller must re-construct aDateexplicitly if needed.JSON.stringify(value, null, 2)(a thirdspaceargument) pretty-prints with indentation — useful for debugging output.JSON.parsethrows aSyntaxErroron malformed input — always expect and handle that when parsing data from an external source likelocalStorage, since it could be missing, cleared by the user, or corrupted.
Common mistakes
- Assuming
JSON.parse(JSON.stringify(x))is a safe universal deep-copy — it silently loses functions,undefinedvalues, andDate/Map/Setinstances, and throws on circular references. Fine for plain, JSON-safe data; not a general-purpose clone. - Calling
JSON.parseon stored data without atry/catch— malformed or missing stored JSON will throw rather than returnnull/undefined.
Runtime notes
JSON.stringify/JSON.parse are standard, identical across every current browser and Node version.