Reputation: 5648
I have some code that is taking any ol' object and trying to output it as JSON. The problem is that in one of these cases it insists there is a circular reference and thus fails.
According to my debugger this isn't the case (but then again, I don't know the traversal logic for this function either so I can't be sure).
What would be great is to have
JSON.stringify say "Converting circular structure to JSON 'foo.baz.bat' " where 'foo.baz.bat' is the first offending reference.
Is there a way to do this without implementing my own custom replacer? For this particular part of the code it's job should ONLY be to format to JSON and absolutely nothing more. Meaning: I don't want to massage the data here if that's what it takes.
To be clear: What makes this different from other posts suggesting that I write a custom replacer to munge-out the data is that I just need this to debug what the offending member is.
Something already built in.
Upvotes: 0
Views: 60
Reputation: 6017
Install json-stringify-safe
And the offending field will be shown as [Circular]
, like this:
{
"circularRef": "[Circular]",
"list": [
"[Circular]",
"[Circular]"
]
}
Upvotes: 1