JSON.parse() and JSON.stringify(): The JavaScript Developer's Guide
2025-08-01 6 min read
JSON.parse and JSON.stringify are used in almost every JavaScript app. This guide covers syntax, error handling, the replacer/reviver patterns, and deep cloning.
JSON.parse() andJSON.stringify()are in almost every JavaScript application โ from reading API responses to storing data in localStorage. Here's everything you need to know to use them correctly.
JSON.parse() โ Converting JSON String to Object
const obj = JSON.parse('{"name":"Alice","age":30}');
// obj.name โ "Alice"
// Always wrap in try/catch โ invalid JSON throws SyntaxError
try {
const data = JSON.parse(response);
} catch (e) {
console.error("Invalid JSON:", e.message);
}JSON.stringify() โ Converting Object to JSON String
const json = JSON.stringify({ name: "Alice", age: 30 });
// โ '{"name":"Alice","age":30}'
// Pretty-print with indent
JSON.stringify(obj, null, 2);
// Custom replacer to filter keys
JSON.stringify(obj, ["name"]); // only include "name"The Reviver Function
// Convert date strings back to Date objects JSON.parse(json, (key, value) => key === "date" ? new Date(value) : value );
Deep Cloning With JSON
// Simple deep clone (loses functions, undefined, symbols) const clone = JSON.parse(JSON.stringify(obj)); // Modern alternative (structuredClone, supported 2022+) const clone = structuredClone(obj);
What JSON Can't Represent
undefinedvalues are omitted- Functions are omitted
InfinityandNaNbecomenull- Circular references throw a TypeError