circular structure to json code example

Example 1: json stringify close circle

const getCircularReplacer = () => {
  const seen = new WeakSet();
  return (key, value) => {
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) {
        return;
      }
      seen.add(value);
    }
    return value;
  };
};

JSON.stringify(circularReference, getCircularReplacer());

Example 2: UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON

I also ran into this issue. It was because I forgot to await for a promise.

Example 3: Converting circular structure to JSON

//The error means that the object you pass in the request has a circular reference, something like:

var a = {};
a.b = a;