35

I'm trying to stringify(...) an object in Chrome, and I keep getting a "Converting circular structure to JSON" message, despite the fact that (as far as I know) no such structure exists.

I've been over the code a dozen times and can't find any circular references whatsoever. Is there any way to get Chrome to tell me what it's bitching about beyond this painfully useless error message?

Mike
  • 2,434
  • 1
  • 16
  • 19
  • 2
    I posted an answer that may help if you get this error in Node.js where circular references cannot be avoided. http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json/9653082#9653082 – Eric Mar 11 '12 at 07:03

2 Answers2

32

Pardon me if this is too obvious. At the time of writing, I dont know what you have tried.

insert

console.log(the object); 

replacing 'the object' with the object you are passing to JSON.stringify()

insert this line before the JSON.stringify call

and look in the console log (shift control J) for the object. In the console log the object will be tagged with a ">" symbol which can be clicked to expand to the fields.

It is complaining about an object that has pointers into itself, like this kind of object:

A = [];
A[0] = A; 
JSON.stringify(A); // circular error
zangw
  • 43,869
  • 19
  • 177
  • 214
Paul
  • 26,170
  • 12
  • 85
  • 119
  • 1
    I was having this problem and could not figure it out by typing in the console. I put `console.log(all)` right before my `JSON.stringify(all)` and I found that the `all` variable is actually used by Google Chrome! I guess my code would override it, but only after the page has loaded since `all` contains all HTML on the page! Thanks for the tip. – styfle Aug 11 '11 at 19:16
  • That sounds odd. I checked all in chrome and it is undef, as expected – Paul Aug 12 '11 at 06:42
  • 1
    Maybe it was a bug? It was happening consistently for me in Chrome 12.0.742.122 m on Windows XP. I changed my variable from `all` to `allData` and everything works fine now. – styfle Aug 12 '11 at 16:55
  • Glad you found a solution. Sounds like a mystery on this end. – Paul Aug 12 '11 at 19:07
  • 1
    great answer, it helped me find the source element that was triggering the event. – Uriel Arvizu Jan 24 '13 at 22:11
  • This does not always fully unpack objects. – abalter Apr 11 '16 at 04:27
2

You can use dojox.json.ref to find circular references. This code prints json representation of your objectWithCircularReferences:

require(["dojox/json/ref"], function(){
    console.log(dojox.json.ref.toJson(objectWithCircularReferences));
});

Any occurence of "$ref" substring in its output to console will help you locate circular references. You can alternatively pipe this json output to global variable ZZZ like this if you wish:

require(["dojox/json/ref"], function(){
    window.ZZZ = dojox.json.ref.toJson(objectWithCircularReferences);
});

And of course you need to include dojo library beforehand. In an html file:

<script src="//yandex.st/dojo/1.9.1/dojo/dojo.js"></script>

In firebug console:

include("//yandex.st/dojo/1.9.1/dojo/dojo.js")

In Chrome console:

SCRIPT = document.createElement('script');
SCRIPT.src = '//yandex.st/dojo/1.9.1/dojo/dojo.js';
document.body.appendChild(SCRIPT);
user2683246
  • 3,399
  • 29
  • 31
  • what is ko.toJS in your code ? i tried dojox.json.ref.toJson( saveObj ) and i still get Uncaught TypeError: Converting circular structure to JSON – max4ever Jan 30 '15 at 16:52
  • @max4ever: `ko.toJS` went from [KnockOut.js library](http://knockoutjs.com/documentation/json-data.html) that I used in my project. But here it's superfluous. I've cleaned the code. Thank you. – user2683246 Jan 30 '15 at 18:21