14

For debugging I want to serialize javascript objects with JSON.stringify(myobject). But this gives:

TypeError: Converting circular structure to JSON

Is there a way to prevent this, by for instance pruning the output tree?

Some more background:

I want to collect some data on different objects and see what is going on, and why a feature works for one situation but not for another. By comparing outputs I hope to be able to find differences which explains why it is not working in "another" situation. I'm using jquery and my debug horse is called chrome. If there are better alternatives for doing this type of debugging activities I'm also very much interested!

Cheers, jeroen.

4 Answers 4

9

JSON.stringify(obj) does not support circular referencing such as:

var car = {}
car.myself = car;
JSON.stringify(car);

However dojox.json.ref does support circular referencing, if you wanted to explore another option.

However if your purposes are strictly to debug, I'd suggest using the built in browser debugger such as Chrome's, IE's or Firebug(for firefox).

Sign up to request clarification or add additional context in comments.

Comments

7

You can use console.log() and the chrome javascript debug console, which will happily let you inspect your object even if it has cyclic references.

Comments

2

For node.js json-ref is a nice lightweight alternative to the dojox.json.ref function suggested by Mike Lewis.

Comments

2

You can now use Douglas Crockford's JSON Stringify plugin:

https://github.com/douglascrockford/JSON-js

This has a decycle option in the download file cycle.js. You could also use console.log() and inspect the JSON in your browsers console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.