# Handling many JSON msg payload

**URL:** https://discourse.nodered.org/t/handling-many-json-msg-payload/73645
**Category:** General
**Tags:** mqtt
**Created:** [13 January 2023 18:37 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645 "2023-01-13T18:37:13Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![rchandrashekar53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rchandrashekar53/32/60423_2.png) [@rchandrashekar53](https://discourse.nodered.org/u/rchandrashekar53)
#### Post date: [13 January 2023 18:37 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/1 "2023-01-13T18:37:13Z")

</div>

Hi, i have multiple MQTT topics and i need to send individual payload at same time to different MQTT topic.

The issue is if i use msg.payload - the JSON frames will be overwritten. I did explore a path where i tried to create msg.test1 and msg.test2 with JSON payload but the issue is how can my MQTT node will not work with this format.

How can i manage this situation.?

code

```auto
[
    {
        "id": "46a7f55b3dc113fa",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f90132623ba1efaa",
        "type": "inject",
        "z": "46a7f55b3dc113fa",
        "name": "inject",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 200,
        "wires": [
            [
                "3120844e6d7228c6",
                "08788246c8d75e3e"
            ]
        ]
    },
    {
        "id": "3120844e6d7228c6",
        "type": "function",
        "z": "46a7f55b3dc113fa",
        "name": "test1",
        "func": "msg.test1={\n\"temperature\" : 20,\n\"pressure\" : 2\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 160,
        "wires": [
            [
                "7a010a252dc12b7b",
                "6264ca3a4d52b995"
            ]
        ]
    },
    {
        "id": "08788246c8d75e3e",
        "type": "function",
        "z": "46a7f55b3dc113fa",
        "name": "test2",
        "func": "msg.test2={\n\"humidity\" : 89,\n\"load\" : 30,\n\"time\" : \"\"\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 240,
        "wires": [
            [
                "d806c099180ea2d3"
            ]
        ]
    },
    {
        "id": "7a010a252dc12b7b",
        "type": "mqtt out",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "topic": "topic1",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "272c1aebb3ef8a9a",
        "x": 610,
        "y": 120,
        "wires": []
    },
    {
        "id": "d806c099180ea2d3",
        "type": "mqtt out",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "topic": "topic2",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "272c1aebb3ef8a9a",
        "x": 590,
        "y": 260,
        "wires": []
    },
    {
        "id": "6264ca3a4d52b995",
        "type": "debug",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "test1",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 560,
        "y": 60,
        "wires": []
    },
    {
        "id": "65c1bfd5cb2f1feb",
        "type": "debug",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "test2",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 520,
        "y": 320,
        "wires": []
    },
    {
        "id": "272c1aebb3ef8a9a",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.29",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [13 January 2023 19:10 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/2 "2023-01-13T19:10:11Z")

</div>

Looking at your functions, your payloads need to be in msg.payload, so you could do.  
e.g

```auto
msg.payload = {
  "test1": {
    "temperature" : 20,
    "pressure" : 2
  }
}

```

[edit] I am also thinking do you want to join the messages before sending to mqtt out

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [13 January 2023 19:13 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/3 "2023-01-13T19:13:44Z")

</div>

Im pretty sure (I don't use MQTT), the MQTT node uses `msg.payload` for the payload.  
therefore anything else won't be used.

This is a deep set standard in Node RED.

Try the below.  
This will ensure each `payload` is not affecting the other

```auto
[{"id":"f90132623ba1efaa","type":"inject","z":"46a7f55b3dc113fa","name":"inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":200,"wires":[["08788246c8d75e3e"]]},{"id":"08788246c8d75e3e","type":"function","z":"46a7f55b3dc113fa","name":"Send Messages","func":"const Message1 = {\n payload: {\n \"temperature\": 20,\n \"pressure\": 2\n },\n topic: 'topic1'\n}\nnode.send(Message1)\n\nconst Message2 = {\n payload: {\n \"humidity\": 89,\n \"load\": 30,\n \"time\": \"\"\n },\n topic: 'topic2'\n}\nnode.send(Message2)\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":200,"wires":[["d806c099180ea2d3"]]},{"id":"d806c099180ea2d3","type":"mqtt out","z":"46a7f55b3dc113fa","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"272c1aebb3ef8a9a","x":575,"y":200,"wires":[]},{"id":"272c1aebb3ef8a9a","type":"mqtt-broker","name":"","broker":"192.168.1.29","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

 ![Screenshot 2023-01-13 at 19.12.12](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f438156ccbb32ad15599411ba193b3b99a8023d6.png)

EDIT  
i.e as @E1cid explains

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 January 2023 19:14 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/4 "2023-01-13T19:14:24Z")

</div>

> [@rchandrashekar53](#):
>
> The issue is if i use msg.payload - the JSON frames will be overwritten.

What makes you say that?  
My understanding is that each message is entirely seperate.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [13 January 2023 19:21 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/5 "2023-01-13T19:21:22Z")

</div>

> [@jbudd](#):
>
> My understanding is that each message is entirely seperate.

I _think_ its referring to reusing `payload` when it may still be in transit.  
given its an object, its passed as a reference

---

<div class="post-metadata">

### Author: ![rchandrashekar53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rchandrashekar53/32/60423_2.png) [@rchandrashekar53](https://discourse.nodered.org/u/rchandrashekar53)
#### Post date: [13 January 2023 19:33 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/6 "2023-01-13T19:33:50Z")

</div>

thanks Marcus - will try and will keep you posted

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 January 2023 19:38 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/7 "2023-01-13T19:38:34Z")

</div>

I admit I'm just making assumptions here, but surely if msg was prone to being overwritten whenever a flow splits into two or more wires we would have heard more people complaining about it?

Can a developer clarify this please?

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [13 January 2023 19:45 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/8 "2023-01-13T19:45:42Z")

</div>

Your right, just tested!  
it could well be constrained to the function block - as I'm pretty sure I have hit this situation my self.

agreed, confirmation will be nice

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [13 January 2023 20:02 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/9 "2023-01-13T20:02:53Z")

</div>

There was a resent issue regarding not defining vars in function nodes, the result was they were available when the function node was called again. That is if i remember correctly

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [13 January 2023 20:14 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/10 "2023-01-13T20:14:38Z")

</div>

There is [this article](https://nodered.org/blog/2019/09/13/cloning-messages) from @knolleary about a change (in NR 1.0 !) to prevent problems with msg due to pass-by-reference.

---

<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: [14 March 2023 20:15 UTC](https://discourse.nodered.org/t/handling-many-json-msg-payload/73645/11 "2023-03-14T20:15:03Z")

</div>

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