Skip to content
the poor coder

converting circular structure to json

Poor Man
Published
Reading time
2 min read
On this page

Converting Circular Structure to JSON#

Have you ever encountered an error message in JavaScript that says "Converting circular structure to JSON"? This error occurs when trying to convert an object that contains circular references, such as when an object references itself in a nested property or when two objects reference each other.

Why Does This Happen?#

When converting an object to JSON, the JSON.stringify() function uses the JSON format to represent the object's properties and their values as a string. However, if the object contains circular references, the function cannot convert it to JSON because it would result in an infinite loop.

How to Fix It?#

Here are some ways to fix this error:

  • Remove Circular References: The easiest way to fix this error is to remove the circular references from the object. This can be done by either removing the property that causes the circular reference or by restructuring the object so that it does not contain circular references.
  • Use a Custom Replacer Function: Another solution is to use the replacer parameter of the JSON.stringify() function to define a custom replacer function that can handle circular references. This function should check whether a value is a circular reference and replace it with a placeholder value that can be later replaced with the original circular reference.
  • Use a Third-Party Library: There are several third-party libraries available that can handle circular references when converting an object to JSON, such as circular-json and flatted. These libraries provide a simple way to convert objects with circular references without encountering the "Converting circular structure to JSON" error.

Example Code#

Text

const obj = {
  prop1: 'value1',
  prop2: {
    nestedProp: 'nestedValue',
    selfReference: null
  }
};

obj.prop2.selfReference = obj.prop2;

// This will throw an error
JSON.stringify(obj);

// Fix 1: Remove the Circular Reference
obj.prop2.selfReference = null;

// This will work
JSON.stringify(obj);

// Fix 2: Use a Custom Replacer Function
const replacer = (key, value) => {
  if (key === 'selfReference') {
    return '[Circular]';
  }
  return value;
};

const jsonString = JSON.stringify(obj, replacer);

// Fix 3: Use a Third-Party Library
const circularJSON = circularJson.stringify(obj);

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC