This question has already been answered:

I have an associative array (object) selectedData When trying to convert it to JSON like this:

 myJson = JSON.stringify(selectedData[0]); 

or

 myJson = JSON.stringify(selectedData[1]); 

and so on, then everything is OK.

But if I need to convert all the elements of an array:

 myJson = JSON.stringify(selectedData); 

Then I get the error:

 Uncaught TypeError: Converting circular structure to JSON 

How can I convert a two-dimensional associative array into JSON without this error?

Reported as a duplicate at Grundy. javascript Mar 26 at 7:57 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • remove circular links and JSON.stringify will work - Stranger in the Q
  • There is something such arr[0]=arr that has no end to any edge - Alexander Lonberg pm

1 answer 1

If you read the error message, you can see that somewhere in the array there are cyclic dependencies. JSON does not support such objects, you must somehow get rid of them.

If it is required to serialize and deserialize the cyclic structure in its original form, then it is possible to transform the serializable object into an array containing information on objects and indices instead of references. The implementation itself is shown in another answer .

  • I did not understand anything at all ... Is it really that easy to solve a problem in JS so difficult? - Coffee inTime
  • 2
    @CoffeeinTime, a simple problem is solved simply, but you have some kind of dregs in the array. - Qwertiy pm