# TypeError: Converting circular structure to JSON

**URL:** <https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039>\
**Category:** General\
**Created:** [29 February 2024 20:14 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039 "2024-02-29T20:14:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![daycare](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daycare/32/88634_2.png) [@daycare](https://discourse.nodered.org/u/daycare)\
**Post date:** [29 February 2024 20:14 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/1 "2024-02-29T20:14:55Z")

</div>

Hi,  
I could use some help and, if possible, a little explanation.

After quite a struggle, I managed to configure a Tuya smart plug and, with [node-red-contrib-tuya-smart-device](https://flows.nodered.org/node/node-red-contrib-tuya-smart-device), the following flow works fine manually:

> **Flow**
>
> ```auto
> [
> {
> "id": "d79d91df60007d08",
> "type": "tuya-smart-device",
> "z": "2b2676f81a6deb7d",
> "deviceName": "Tuya Smart PLug",
> "disableAutoStart": false,
> "deviceId": "",
> "deviceKey": "",
> "storeAsCreds": true,
> "deviceIp": "192.168.42.189",
> "retryTimeout": "1001",
> "findTimeout": "2000",
> "tuyaVersion": "3.1",
> "eventMode": "event-both",
> "x": 930,
> "y": 800,
> "wires": [
> [],
> []
> ]
> },
> {
> "id": "2a21969c0c2b6363",
> "type": "function",
> "z": "2b2676f81a6deb7d",
> "name": "On/Off",
> "func": "msg.payload = {\n \"dps\": 1,\n \"set\": msg.payload\n}\nreturn msg;",
> "outputs": 1,
> "timeout": 0,
> "noerr": 0,
> "initialize": "",
> "finalize": "",
> "libs": [],
> "x": 710,
> "y": 800,
> "wires": [
> [
> "d79d91df60007d08"
> ]
> ]
> },
> {
> "id": "2435c40669d586b8",
> "type": "inject",
> "z": "2b2676f81a6deb7d",
> "name": "Turn On",
> "props": [
> {
> "p": "payload"
> }
> ],
> "repeat": "",
> "crontab": "",
> "once": false,
> "onceDelay": 0.1,
> "topic": "",
> "payload": "true",
> "payloadType": "bool",
> "x": 520,
> "y": 780,
> "wires": [
> [
> "2a21969c0c2b6363"
> ]
> ]
> },
> {
> "id": "43b7394eecac0612",
> "type": "inject",
> "z": "2b2676f81a6deb7d",
> "name": "Turn Off",
> "props": [
> {
> "p": "payload"
> },
> {
> "p": "topic",
> "vt": "str"
> }
> ],
> "repeat": "",
> "crontab": "",
> "once": false,
> "onceDelay": 0.1,
> "topic": "",
> "payload": "false",
> "payloadType": "bool",
> "x": 510,
> "y": 820,
> "wires": [
> [
> "2a21969c0c2b6363"
> ]
> ]
> }
> ]
> 
> ```

So I set out to make it work with a voice command.  
It seemed simple enough, so I simply did this:

> **Try...**
>
> ```auto
> [
> {
> "id": "e2f84d07b8024614",
> "type": "switch",
> "z": "2b2676f81a6deb7d",
> "name": "tuya - ON",
> "property": "payload.intent.name",
> "propertyType": "msg",
> "rules": [
> {
> "t": "eq",
> "v": "tuyaON",
> "vt": "str"
> }
> ],
> "checkall": "true",
> "repair": false,
> "outputs": 1,
> "x": 500,
> "y": 940,
> "wires": [
> [
> "318239d590da3a83"
> ]
> ]
> },
> {
> "id": "14d0beacaa37b179",
> "type": "switch",
> "z": "2b2676f81a6deb7d",
> "name": "tuya - OFF",
> "property": "payload.intent.name",
> "propertyType": "msg",
> "rules": [
> {
> "t": "eq",
> "v": "tuyaOFF",
> "vt": "str"
> }
> ],
> "checkall": "true",
> "repair": false,
> "outputs": 1,
> "x": 510,
> "y": 980,
> "wires": [
> [
> "77744660066f4cd9"
> ]
> ]
> },
> {
> "id": "f697fd60bbd64d66",
> "type": "function",
> "z": "2b2676f81a6deb7d",
> "name": "On/Off",
> "func": "msg.payload = {\n \"dps\": 1,\n \"set\": msg.payload\n}\nreturn msg;",
> "outputs": 1,
> "timeout": 0,
> "noerr": 0,
> "initialize": "",
> "finalize": "",
> "libs": [],
> "x": 1030,
> "y": 960,
> "wires": [
> [
> "e920facd44b706fa"
> ]
> ]
> },
> {
> "id": "e920facd44b706fa",
> "type": "tuya-smart-device",
> "z": "2b2676f81a6deb7d",
> "deviceName": "Tuya Smart PLug",
> "disableAutoStart": false,
> "deviceId": "",
> "deviceKey": "",
> "storeAsCreds": true,
> "deviceIp": "192.168.42.189",
> "retryTimeout": "1001",
> "findTimeout": "2000",
> "tuyaVersion": "3.1",
> "eventMode": "event-both",
> "x": 1230,
> "y": 960,
> "wires": [
> [],
> []
> ]
> },
> {
> "id": "318239d590da3a83",
> "type": "change",
> "z": "2b2676f81a6deb7d",
> "name": "",
> "rules": [
> {
> "t": "set",
> "p": "payload.intent.name",
> "pt": "msg",
> "to": "true",
> "tot": "bool"
> }
> ],
> "action": "",
> "property": "",
> "from": "",
> "to": "",
> "reg": false,
> "x": 780,
> "y": 940,
> "wires": [
> [
> "f697fd60bbd64d66"
> ]
> ]
> },
> {
> "id": "77744660066f4cd9",
> "type": "change",
> "z": "2b2676f81a6deb7d",
> "name": "",
> "rules": [
> {
> "t": "set",
> "p": "payload.intent.name",
> "pt": "msg",
> "to": "false",
> "tot": "bool"
> }
> ],
> "action": "",
> "property": "",
> "from": "",
> "to": "",
> "reg": false,
> "x": 780,
> "y": 980,
> "wires": [
> [
> "f697fd60bbd64d66"
> ]
> ]
> }
> ]
> 
> ```
> 
> ```auto
> 
> ```

But I think the payload is not converted to the right format, because I get the error :

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

```

What is this subtlety that escapes the clumsy beginner in me?

Thanks

---

<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:** [29 February 2024 21:12 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/2 "2024-02-29T21:12:23Z")

</div>

> [@daycare](#):
>
> But I think the payload is not converted to the right format, because I get the error :
> 
> ```auto
> TypeError: Converting circular structure to JSON
> --> starting at object with constructor 'Socket'
> | property 'parser' -> object with constructor 'HTTPParser'
> --- property 'socket' closes the circle
> 
> ```
> 
> What is this subtlety that escapes the clumsy beginner in me?

I don't use that node so cannot comment on the format of data however it is related to the tuya node: [Version 4.1.1 error Base64 string · Issue #66 · vinodsr/node-red-contrib-tuya-smart-device · GitHub](https://github.com/vinodsr/node-red-contrib-tuya-smart-device/issues/66)

Looking at your function:

```auto
msg.payload = {
 "dps": 1,
 "set": msg.payload
}
return msg

```

I can see how you are creating the circular object (assuming msg.payload going in to the function is an object)

If you really mean to include the data from the incoming msg payload into the payload of the outgoing msg them you could clone it e.g...

```auto
const clone = RED.util.cloneMessage(msg.payload)
msg.payload = {
 "dps": 1,
 "set": clone
}
return msg

```

---

<div class="post-metadata">

**Author:** ![daycare](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daycare/32/88634_2.png) [@daycare](https://discourse.nodered.org/u/daycare)\
**Post date:** [29 February 2024 21:39 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/3 "2024-02-29T21:39:46Z")

</div>

> [@Steve-Mcl](#):
>
> If you really mean to include the data from the incoming msg payload into the payload of the outgoing msg them

I don't know if this is the right approach, but my aim is just to inject a "true" payload into the function, as the manual injection via "Turn ON/OFF" does.  
I don't understand the difference between this manuel payload injection, which works, and the one I get via the "change" node, in which I was thinking of giving it this boolean "true" value.

I hope I'm making myself clear...

Otherwise, I've tried to replace my function with the one you're giving me, but I get the same error message.

Thanks for your time

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [29 February 2024 21:46 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/4 "2024-02-29T21:46:12Z")

</div>

Put a` debug node` after the `change node` and check that you are actually getting `msg.payload = true / false`

---

<div class="post-metadata">

**Author:** ![daycare](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daycare/32/88634_2.png) [@daycare](https://discourse.nodered.org/u/daycare)\
**Post date:** [29 February 2024 21:53 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/5 "2024-02-29T21:53:15Z")

</div>

No, that's not what I get...  
(intent is in French : "switch on the plug")

> **Summary**
>
> ```auto
> 29/02/2024 22:50:39noeud: debug 12
> msg.payload : Object
> object
> intent: object
> name: true
> confidence: 1
> entities: array[0]
> slots: object
> empty
> text: "allume la prise"
> raw_text: "allume la prise"
> tokens: array[3]
> 0: "allume"
> 1: "la"
> 2: "prise"
> raw_tokens: array[3]
> 0: "allume"
> 1: "la"
> 2: "prise"
> wakeword_id: "jarvis_raspberry-pi"
> site_id: "Rhasspy"
> 
> ```

---

<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:** [29 February 2024 21:56 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/6 "2024-02-29T21:56:21Z")

</div>

Assuming you are supposed to be sending true or false, then that's your issue.

---

<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:** [29 February 2024 21:58 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/7 "2024-02-29T21:58:17Z")

</div>

Ps, please use the copy value button when copying messages/payloads and please use code blocks

### copying data

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

### posting code

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![daycare](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daycare/32/88634_2.png) [@daycare](https://discourse.nodered.org/u/daycare)\
**Post date:** [29 February 2024 23:19 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/8 "2024-02-29T23:19:26Z")

</div>

> [@Steve-Mcl](#):
>
> Ps, please use the copy value button when copying messages/payloads and please use code blocks

Sorry for the mess, I'll be more rigorous.

So I'm really missing something...

**The manual flow**

> **Summary**
>
> ```auto
> [
> {
> "id": "d79d91df60007d08",
> "type": "tuya-smart-device",
> "z": "2b2676f81a6deb7d",
> "d": true,
> "deviceName": "Tuya Smart PLug",
> "disableAutoStart": false,
> "deviceId": "",
> "deviceKey": "",
> "storeAsCreds": true,
> "deviceIp": "192.168.42.189",
> "retryTimeout": "1001",
> "findTimeout": "2000",
> "tuyaVersion": "3.1",
> "eventMode": "event-both",
> "x": 930,
> "y": 800,
> "wires": [
> [],
> []
> ]
> },
> {
> "id": "2a21969c0c2b6363",
> "type": "function",
> "z": "2b2676f81a6deb7d",
> "d": true,
> "name": "On/Off",
> "func": "msg.payload = {\n \"dps\": 1,\n \"set\": msg.payload\n}\nreturn msg;",
> "outputs": 1,
> "timeout": 0,
> "noerr": 0,
> "initialize": "",
> "finalize": "",
> "libs": [],
> "x": 730,
> "y": 800,
> "wires": [
> [
> "d79d91df60007d08",
> "837be6af93271176"
> ]
> ]
> },
> {
> "id": "2435c40669d586b8",
> "type": "inject",
> "z": "2b2676f81a6deb7d",
> "d": true,
> "name": "Turn On",
> "props": [
> {
> "p": "payload"
> }
> ],
> "repeat": "",
> "crontab": "",
> "once": false,
> "onceDelay": 0.1,
> "topic": "",
> "payload": "true",
> "payloadType": "bool",
> "x": 520,
> "y": 780,
> "wires": [
> [
> "303cac8eee413937",
> "2a21969c0c2b6363"
> ]
> ]
> },
> {
> "id": "43b7394eecac0612",
> "type": "inject",
> "z": "2b2676f81a6deb7d",
> "d": true,
> "name": "Turn Off",
> "props": [
> {
> "p": "payload"
> },
> {
> "p": "topic",
> "vt": "str"
> }
> ],
> "repeat": "",
> "crontab": "",
> "once": false,
> "onceDelay": 0.1,
> "topic": "",
> "payload": "false",
> "payloadType": "bool",
> "x": 510,
> "y": 820,
> "wires": [
> [
> "2a21969c0c2b6363"
> ]
> ]
> },
> {
> "id": "303cac8eee413937",
> "type": "debug",
> "z": "2b2676f81a6deb7d",
> "d": true,
> "name": "debug 11",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "false",
> "statusVal": "",
> "statusType": "auto",
> "x": 700,
> "y": 740,
> "wires": []
> },
> {
> "id": "837be6af93271176",
> "type": "debug",
> "z": "2b2676f81a6deb7d",
> "d": true,
> "name": "debug 14",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "false",
> "statusVal": "",
> "statusType": "auto",
> "x": 940,
> "y": 740,
> "wires": []
> }
> ]
> 
> ```

Give this

```auto
01/03/2024 00:11:51 [noeud: debug 11]
msg.payload : boolean
true

01/03/2024 00:11:51[noeud: debug 14]
msg.payload : Object

object
dps: 1
set: true

```

**The voice command**

> **Summary**
>
> ```auto
> [
> {
> "id": "153b40c3716557f4",
> "type": "function",
> "z": "2b2676f81a6deb7d",
> "name": "On/Off",
> "func": "msg.payload = {\n \"dps\": 1,\n \"set\": msg.payload\n}\nreturn msg;",
> "outputs": 1,
> "timeout": 0,
> "noerr": 0,
> "initialize": "",
> "finalize": "",
> "libs": [],
> "x": 730,
> "y": 980,
> "wires": [
> [
> "60c8606a7013a1fc",
> "a0cc2f0a7d98d338"
> ]
> ]
> },
> {
> "id": "a0cc2f0a7d98d338",
> "type": "tuya-smart-device",
> "z": "2b2676f81a6deb7d",
> "deviceName": "Tuya Smart PLug",
> "disableAutoStart": false,
> "deviceId": "",
> "deviceKey": "",
> "storeAsCreds": true,
> "deviceIp": "192.168.42.189",
> "retryTimeout": "1001",
> "findTimeout": "2000",
> "tuyaVersion": "3.1",
> "eventMode": "event-both",
> "x": 1150,
> "y": 980,
> "wires": [
> [],
> []
> ]
> },
> {
> "id": "60c8606a7013a1fc",
> "type": "debug",
> "z": "2b2676f81a6deb7d",
> "name": "debug 16",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "false",
> "statusVal": "",
> "statusType": "auto",
> "x": 940,
> "y": 940,
> "wires": []
> },
> {
> "id": "167827b4722ff60b",
> "type": "debug",
> "z": "2b2676f81a6deb7d",
> "name": "debug 17",
> "active": true,
> "tosidebar": true,
> "console": false,
> "tostatus": false,
> "complete": "false",
> "statusVal": "",
> "statusType": "auto",
> "x": 740,
> "y": 940,
> "wires": []
> },
> {
> "id": "6eaf08e4d166d62c",
> "type": "change",
> "z": "2b2676f81a6deb7d",
> "name": "",
> "rules": [
> {
> "t": "set",
> "p": "payload",
> "pt": "msg",
> "to": "true",
> "tot": "bool"
> }
> ],
> "action": "",
> "property": "",
> "from": "",
> "to": "",
> "reg": false,
> "x": 540,
> "y": 980,
> "wires": [
> [
> "167827b4722ff60b",
> "153b40c3716557f4"
> ]
> ]
> }
> ]
> 
> ```

Give this

```auto
01/03/2024 00:15:50 [noeud: debug 17]
msg.payload : boolean
true

01/03/2024 00:15:50[noeud: debug 16]
msg.payload : Object

object
dps: 1
set: true

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:** [1 March 2024 01:36 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/9 "2024-03-01T01:36:12Z")

</div>

Ok, I think somewhere there must be a http request in your flow and your msg has a .res object. That tuya node is probably cloning (attempting to clone) the whole message but is NOT using the built in cloning routine and therefore falling foul of this fact.

Try setting the function to...

```auto
const m = {
  payload: {
    dps: 1,
    set: msg.payload
  }
}
return m

```

Note: if this works, you should report this to the node author at the link I provided in my first reply.

---

<div class="post-metadata">

**Author:** ![daycare](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daycare/32/88634_2.png) [@daycare](https://discourse.nodered.org/u/daycare)\
**Post date:** [1 March 2024 07:45 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/10 "2024-03-01T07:45:42Z")

</div>

Indeed, it works.

I'm having trouble understanding your explanation and I'm not sure I can clearly present the problem to the plugin's author, but at worst I'll provide him with a link to this topic.

Thanks a lot for your help !

---

<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:** [15 March 2024 07:46 UTC](https://discourse.nodered.org/t/typeerror-converting-circular-structure-to-json/86039/11 "2024-03-15T07:46:42Z")

</div>

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