0

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.

1

1 Answer 1

1

Install json-stringify-safe

And the offending field will be shown as [Circular], like this:

{ "circularRef": "[Circular]", "list": [ "[Circular]", "[Circular]" ] } 
Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I was looking for. Can I get an up vote on the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.