Converting circular structure to JSON -- Any way to find what field it is complaining about? Converting circular structure to JSON -- Any way to find what field it is complaining about? google-chrome google-chrome

Converting circular structure to JSON -- Any way to find what field it is complaining about?


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


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);