2

Why does the following piece of code lead to "Type Error: Converting Circular Structure to JSON" in Chrome console ?

JSON.stringify(document.querySelectorAll("img")) 
1
  • Can you give some more details ?...i.e you want id of an img or ???? Commented Dec 1, 2014 at 6:33

1 Answer 1

3

A DOM element contains circular references and you can't call JSON.stringify() on any object that contains circular references or contains objects with circular references.

For example, it contains .parentNode which then contains .childNodes which then points right back to the same element. As soon as JSON.stringify() detects such a circular reference, it stops because it can't record all the properties.

Basically, you can't stringify a DOM node or a list of DOM nodes. Perhaps if you help us understand what problem you're really trying to solve, we can help with an alternative way of doing this. For example, maybe all you need to save it the img.src property in order to save these images.

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

2 Comments

I've never fully understood this error myself. Thanks for the good example (parentNode/childNodes)
So I have used Array.prototype.map to create another array that contains just the height, width and src. I could JSONify it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.