# How to resolve this error TypeError: Converting circular structure to JSON

**URL:** <https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248>\
**Category:** General\
**Created:** [27 January 2023 07:48 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248 "2023-01-27T07:48:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 07:48 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/1 "2023-01-27T07:48:41Z")

</div>

TypeError: Converting circular structure to JSON  
--\> starting at object with constructor 'Socket'  
| property 'parser' -\> object with constructor 'HTTPParser'  
--- property 'socket' closes the circle

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 January 2023 07:53 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/2 "2023-01-27T07:53:55Z")

</div>

What are you doing when this happens? (show us your flow)

Where do you see this error? (in console? in debug panel?)

What version of NodeJS and Node-RED are you running?

What platform are you running on?

---

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 10:39 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/3 "2023-01-27T10:39:42Z")

</div>

iam trying to write the api response into a parquet file.

1. i recived the api result in the form of array msg.payload.organic\_result.
2. i redeclared a new array and parsed the result into this new array and again reassigned it to msg.payload beacuse (write node) it requires input in the form of an array buffer.  
 ![Screenshot (16)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/6313a72950bd64c44c2995159b8364d4bf063333.png)  
 ![Screenshot (17)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/0024cb1fc71a22ba1aaa3c6945960cd2cfbff69d.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 January 2023 10:46 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/4 "2023-01-27T10:46:33Z")

</div>

And the other questions?

> [@Steve-Mcl](#):
>
> Where do you see this error? (in console? in debug panel?)
> 
> What version of NodeJS and Node-RED are you running?
> 
> What platform are you running on?

TBH, I think this is related to the `http-in --> http-request --> http-response` - since they both use `msg.req` and `msg.res`

The `msg` from `http-in` must be ultimately passed back to the `http-response` without being destroyed/changed/lost - however when you pass the `msg` to a `http-request` I think that is where the error occurs (I am guessing since you have not answered "Where do you see this error? (in console? in debug panel?)"

FYI: If you see an error in the debug panel, you can "mouse over" it and the failing node will blink!

---

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 11:00 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/5 "2023-01-27T11:00:58Z")

</div>

iam seeing this error in debug panel.  
over the file structure nod

 ![Screenshot (17)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/0024cb1fc71a22ba1aaa3c6945960cd2cfbff69d.png)  
 ![Screenshot (18)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db4072143f23632437e5010f5d5a64fc3fa1162f.png)  
e

---

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 11:02 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/6 "2023-01-27T11:02:11Z")

</div>

platform windows  
version info is given in screen shot

 ![Screenshot (20)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e136b339043bf359c846f249276c997aed6d80b1.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 January 2023 11:08 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/7 "2023-01-27T11:08:30Z")

</div>

Hi @Manish131rai

what exactly is the `file structure` node? Is that a regular `Write File` node? Or is it something else you have installed?

As @Steve-Mcl alluded to, the fact the message being received by that node comes from an `HTTP In` node originally, it will have `msg.req` and `msg.res` set on it. These are the request/response objects for the original HTTP request that triggered the flow. These properties cannot be JSON encoded - for exactly the error you are hitting.

So the better approach would be to change your flow to not try encoding the whole message - just encode the bit you want, such as `msg.payload`. You'll need to keep `msg.res` on the message as that is needed by the `HTTP Response` node you must have somewhere in your flow to respond to the original http request.

---

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 11:18 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/8 "2023-01-27T11:18:21Z")

</div>

file structure node contains coulmn head and the type of data it can hold and it should be similar to that of the data in the array.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 January 2023 11:22 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/9 "2023-01-27T11:22:59Z")

</div>

Hi @Manish131rai

You have not answered the questions I asked. It makes it hard for us to help if we have to keep asking the same questions to get them answered.

Is the File Structure node:

1. a node you have created yourself?
2. a core Node-RED node that you have just set the name of to 'file structure'?
3. a node you have installed yourself? If so, what module did you install to get it?

I have given an explanation of why you are hitting this error. I have made a couple suggestions on how to fix your flow. But I cannot be any more specific because of the missing information.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 January 2023 11:29 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/10 "2023-01-27T11:29:58Z")

</div>

> [@Manish131rai](#):
>
> ![Screenshot (18)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db4072143f23632437e5010f5d5a64fc3fa1162f.png)

That looks like that node is doing something with the full msg. Try sending a new msg with the required payload...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/8681fab20f1a94c7e7510ccdf4cb13d8ad590e41.png)

### PS

It is FAR better to paste code so that we dont have to screenshot and overwrite with boxes and arrows.  
Plus, screenshots are not searchable! They are fine for showing us what you have, but please paste code as text in a `</>` formatted code block. Thanks.

---

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 11:53 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/11 "2023-01-27T11:53:29Z")

</div>

Hi @knolleary  
i'am really sorry for the inconvenience actually i'am very new to node-red ,from next time i'll keep that  
in mind and thanks for answering my queries.  
i have installed the node from here:

> **[@msigrupo-develop/node-red-contrib-parquet](https://flows.nodered.org/node/@msigrupo-develop/node-red-contrib-parquet)**
>
> Read and write parquet format files

---

<div class="post-metadata">

**Author:** ![Manish131rai](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Manish131rai](https://discourse.nodered.org/u/Manish131rai)\
**Post date:** [27 January 2023 12:30 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/14 "2023-01-27T12:30:38Z")

</div>

hey i tried your solution but the file structure node is unable to write the array into the file.

```javascript
let length = msg.payload.organic_results;
const arr=[];

for(let i=0;i<length;i++){
    const obj2={
        "position" : msg.payload.organic_results[i].position,
        "title": msg.payload.organic_results[i].title,
        "link": msg.payload.organic_results[i].link

    };
    arr[i]=obj2;
}
const newMsg={payload : arr};
return newMsg;

```

when it is passed to -------------\>file structure node (it is taking payload as empty array)  
`{"payload":[],"_msgid":"31870f549f46772d"}`

**ADMIN EDIT:** Wrapped code with 3 ````` backticks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 January 2023 13:04 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/15 "2023-01-27T13:04:32Z")

</div>

Add debug nodes BEFORE and AFTER to see if the right stuff goes into your function and the msg is correct afterwards.

To determine what is happening inside your function, use `node.warn(...)` ...

```auto
let length = msg.payload.organic_results;
node.warn( ["length", length] )
const arr=[];

for(let i=0;i<length;i++){
    const obj2={
        "position" : msg.payload.organic_results[i].position,
        "title": msg.payload.organic_results[i].title,
        "link": msg.payload.organic_results[i].link

    };
    node.warn( ["setting new object into arr[index]", i, obj2] )
    arr[i]=obj2;
}
const newMsg={payload : arr};
node.warn( ["Created new msg object - inspect it", newMsg] )
return newMsg;

```

_(untested - may be typos)_

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 February 2023 13:05 UTC](https://discourse.nodered.org/t/how-to-resolve-this-error-typeerror-converting-circular-structure-to-json/74248/16 "2023-02-10T13:05:08Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
