HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Convert a JSON object with circular references to a JavaScript string

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
objectjavascriptwithcircularjsonconvertreferencesstring

Problem

A JSON object is said to have a circular reference when it contains a reference to itself. This can happen when an object is nested within itself, or when two or more objects reference each other. When you try to serialize a JSON object with circular references using JSON.stringify(), you'll get a TypeError because the method can't handle circular references.
In order to handle this, you can traverse the object and use a WeakSet to store and check seen values, and a custom replacer function to omit values already in the WeakSet. This will allow you to serialize the JSON object without running into a TypeError.
> [!WARNING]
>
> This function finds and removes circular references, which causes circular data loss in the serialized JSON. Be sure to handle this data loss appropriately in your application.

Solution

const stringifyCircularJSON = obj => {
  const seen = new WeakSet();
  return JSON.stringify(obj, (k, v) => {
    if (v !== null && typeof v === 'object') {
      if (seen.has(v)) return;
      seen.add(v);
    }
    return v;
  });
};

const obj = { n: 42 };
obj.obj = obj;
stringifyCircularJSON(obj); // '{"n": 42}'


> [!WARNING]
>
> This function finds and removes circular references, which causes circular data loss in the serialized JSON. Be sure to handle this data loss appropriately in your application.

Code Snippets

const stringifyCircularJSON = obj => {
  const seen = new WeakSet();
  return JSON.stringify(obj, (k, v) => {
    if (v !== null && typeof v === 'object') {
      if (seen.has(v)) return;
      seen.add(v);
    }
    return v;
  });
};

const obj = { n: 42 };
obj.obj = obj;
stringifyCircularJSON(obj); // '{"n": 42}'

Context

From 30-seconds-of-code: stringify-circular-json

Revisions (0)

No revisions yet.