Serializing object that contains cyclic object value Serializing object that contains cyclic object value json json

Serializing object that contains cyclic object value


Use the second parameter of stringify, the replacer function, to exclude already serialized objects:

var seen = [];JSON.stringify(obj, function(key, val) {   if (val != null && typeof val == "object") {        if (seen.indexOf(val) >= 0) {            return;        }        seen.push(val);    }    return val;});

http://jsfiddle.net/mH6cJ/38/

As correctly pointed out in other comments, this code removes every "seen" object, not only "recursive" ones.

For example, for:

a = {x:1};obj = [a, a];

the result will be incorrect. If your structure is like this, you might want to use Crockford's decycle or this (simpler) function which just replaces recursive references with nulls:

function decycle(obj, stack = []) {    if (!obj || typeof obj !== 'object')        return obj;        if (stack.includes(obj))        return null;    let s = stack.concat([obj]);    return Array.isArray(obj)        ? obj.map(x => decycle(x, s))        : Object.fromEntries(            Object.entries(obj)                .map(([k, v]) => [k, decycle(v, s)]));}//let a = {b: [1, 2, 3]}a.b.push(a);console.log(JSON.stringify(decycle(a)))