JSON.stringify deep objects

I did what I initially feared I’ll have to do : I took Crockford’s code and modified it for my needs. Now it builds JSON but handles cycles too deep objects too long arrays exceptions (accessors that can’t legally be accessed) In case anybody needs it, I made a GitHub repository : JSON.prune on GitHub … Read more

Stringify (convert to JSON) a JavaScript object with circular reference

Circular structure error occurs when you have a property of the object which is the object itself directly (a -> a) or indirectly (a -> b -> a). To avoid the error message, tell JSON.stringify what to do when it encounters a circular reference. For example, if you have a person pointing to another person … Read more

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 … Read more