How to solve this Javascript error when converting JSON to string?

Uncaught TypeError: Converting circular structure to JSON 

The object I'm trying to execute is this (I registered it in the javascript console):

 Object GsearchResultClass: "GlocalSearch" accuracy: "8" addressLines: Array[2] city: "Cupertino" content: "" country: "United States" ddUrl: "http://www.google.com/maps?source=uds&daddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…" ddUrlFromHere: "http://www.google.com/maps?source=uds&saddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…" ddUrlToHere: "http://www.google.com/maps?source=uds&daddr=10825+North+Wolfe+Road,+Cupertino,+CA+(Southland+Flavor+…" html: HTMLDivElement lat: "37.335405" listingType: "local" lng: "-122.015386" maxAge: 604800 phoneNumbers: Array[1] region: "CA" staticMapUrl: "http://maps.google.com/maps/api/staticmap?maptype=roadmap&format=gif&sensor=false&size=150x100&zoom=…" streetAddress: "10825 North Wolfe Road" title: "Southland Flavor Cafe" titleNoFormatting: "Southland Flavor Cafe" url: "http://www.google.com/maps/place?source=uds&q=stinky&cid=9384294304761453216" viewportmode: "computed" __proto__: Object 

And I do it like this:

 JSON.stringify(theobject); 
+4
source share
3 answers

The object is referencing somewhere; hence the message β€œcircular structure”. I suspect this might be in HTMLDivElement . Do you use this for debugging purposes only or do you really want to do something meaningful in this JSON? If you just use it for debugging, most modern JavaScript debuggers allow you to simply register an object in the console. If you are really trying to do something with the data, you should extract only what you need from this object and place them in a new cropped object that you can pass to JSON.stringify . This object looks like it appeared in the Google API and contains a lot of additional data.

If you do not mind the destructive modification of the object, try to selectively null the suspicious fields and see if JSON.stringify will accept the object. At least you will understand what it causes. Please note: if you do this, you may end up breaking the object for any future use.

+7
source

If the same problem happened, it turned out I was an idiot and forgot .val () at the end of the element that I wanted to insert into the JSON object.

Make sure you do not insert the entire element into the JSON object, otherwise it will try to JSON encode something invalid JSON.

+2
source

I would look at the following

html: HTMLDivElement should probably take the html of the element, not the element itself

or following

Object β†’ proto β†’ Object β†’ proto β†’ Object β†’ ........

0
source

Source: https://habr.com/ru/post/1339139/


All Articles