Course contentsShow
← JavaScript
Lesson 2397 of 2,72349. Common Patterns and IdiomsFree lesson

JSON.parse(JSON.stringify()) Method

Using JSON round-tripping for deep cloning and understanding its limitations with functions, dates, and symbols.

JSON.parse(JSON.stringify()) Method

You'll learn how to use JSON round-tripping for quick deep cloning and understand where it breaks down.

What Is JSON Round-Tripping?

The pattern JSON.parse(JSON.stringify(obj)) is a quick-and-dirty technique for creating a deep copy of an object. It works by converting the object to a JSON string, then parsing that string back into a new object. Since JSON doesn't preserve references, you get a completely independent copy.

Think of it like taking a photograph of a building (stringify), then reconstructing an exact replica from that photo (parse). The new building looks identical but is entirely separate from the original.

How It Works

const original = {
  name: "Alice",
  address: { city: "Boston" }
};

const clone = JSON.parse(JSON.stringify(original));

clone.address.city = "Denver";
console.log(original.address.city); // "Boston" — original untouched

The clone is fully independent. Changes to nested objects don't affect the original.

Critical Limitations

JSON round-tripping cannot handle:

  • Functions: They disappear entirely
  • Dates: Converted to strings, lose Date methods
  • Symbols: Omitted completely
  • undefined: Properties become missing
  • Special objects: RegExp, Map, Set lose their behavior
  • Circular references: Throws an error
const data = {
  func: () => "hi",
  date: new Date(),
  sym: Symbol("id")
};

const clone = JSON.parse(JSON.stringify(data));
console.log(clone); // { date: "2024-..." } — func and sym gone!

When to Use It

Use this pattern for simple data structures — plain objects and arrays containing primitives, other plain objects, and arrays. For anything more complex, you need a proper deep clone library or the structuredClone() method (if available).

Key Takeaway: JSON.parse(JSON.stringify()) provides fast deep cloning for simple data but silently mangles functions, dates, symbols, and complex types.