JavaScript TypeError – Cyclic object value

This JavaScript exception cyclic object value occurs if the references of objects were found in JSON. JSON.stringify() fails to solve them.

Message:

TypeError: cyclic object value (Firefox)
TypeError: Converting circular structure to JSON 
           (Chrome and Opera)
TypeError: Circular reference in value argument 
           not supported (Edge)

Error Type:

TypeError

Cause of error: If in the code references are found then JSON.stringify() fails to solve them.

Example 1: In this example, the circObj has a reference to itself, So the error has occurred.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var circObj = {1: "1"};
    circObj.myself = circObj;
    JSON.stringify(circObj);
</script>

chevron_right


Output:

TypeError: Converting circular structure to JSON

Example 2: In this example, the GFG_Obj has a reference to itself, and JSON.stringify() fails to solve it. So the error has occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var GFG_Obj = {property_1: "value_1"};
    GFG_Obj.myself = GFG_Obj;
    JSON.stringify(GFG_Obj);
</script>

chevron_right


Output:

TypeError: Converting circular structure to JSON

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.