How can I print a circular structure in a JSON-like format? How can I print a circular structure in a JSON-like format? javascript javascript

How can I print a circular structure in a JSON-like format?


In Node.js, you can use util.inspect(object). It automatically replaces circular links with "[Circular]".


Albeit being built-in (no installation is required), you must import it

import * as util from 'util' // has no default exportimport { inspect } from 'util' // or directly// or var util = require('util')
To use it, simply call
console.log(util.inspect(myObject))

Also be aware that you can pass options object to inspect (see link above)

inspect(myObject[, options: {showHidden, depth, colors, showProxy, ...moreOptions}])


Please, read and give kudos to commenters below...


Use JSON.stringify with a custom replacer. For example:

// Demo: Circular referencevar circ = {};circ.circ = circ;// Note: cache should not be re-used by repeated calls to JSON.stringify.var cache = [];JSON.stringify(circ, (key, value) => {  if (typeof value === 'object' && value !== null) {    // Duplicate reference found, discard key    if (cache.includes(value)) return;    // Store value in our collection    cache.push(value);  }  return value;});cache = null; // Enable garbage collection

The replacer in this example is not 100% correct (depending on your definition of "duplicate"). In the following case, a value is discarded:

var a = {b:1}var o = {};o.one = a;o.two = a;// one and two point to the same object, but two is discarded:JSON.stringify(o, ...);

But the concept stands: Use a custom replacer, and keep track of the parsed object values.

As a utility function written in es6:

// safely handles circular referencesJSON.safeStringify = (obj, indent = 2) => {  let cache = [];  const retVal = JSON.stringify(    obj,    (key, value) =>      typeof value === "object" && value !== null        ? cache.includes(value)          ? undefined // Duplicate reference found, discard key          : cache.push(value) && value // Store value in our collection        : value,    indent  );  cache = null;  return retVal;};// Example:console.log('options', JSON.safeStringify(options))


I wonder why nobody posted the proper solution from MDN page yet...

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());

Seen values should be stored in a set, not in array (replacer gets called on every element) and there is no need to try JSON.stringify each element in the chain leading to a circular reference.

Like in the accepted answer, this solution removes all repeating values, not just the circular ones. But at least it does not have exponential complexity.