I have a big object I want to convert to JSON and send. However it has circular structure, so if I try to use JSON.stringify() I’ll get:
TypeError: Converting circular structure to JSON
or
TypeError: cyclic object value
I want to toss whatever circular references exist and send whatever can be stringified. How do I do that?
Thanks.
var obj = {
a: "foo",
b: obj
}
I want to stringify obj into:
{"a":"foo"}
Use
JSON.stringifywith a custom replacer. For example:The replacer in this example is not 100% correct (depending on your definition of “duplicate”). In the following case, a value is discarded:
But the concept stands: Use a custom replacer, and keep track of the parsed object values.
As a utility function written in es6: