“Converting circular structure to JSON” Exception in JavaScript

“Converting circular structure to JSON” Exception in JavaScript

Sample code to Reproduce the issue:

let obj = {};
obj.a = "A";
obj.b = obj;
console.log( JSON.stringify( obj ) );

JSON.stringify() method does not support the circular references. So we have to remove the circular references before converting the object to JSON.

Fix for the above code:

let obj = {};
let obj1 = { "value" : "B" };
obj.a = "A";
obj.b = obj1;
console.log( JSON.stringify( obj ) );

Leave a Reply