# Add an object to a payload

**URL:** <https://discourse.nodered.org/t/add-an-object-to-a-payload/39077>\
**Category:** General\
**Created:** [13 January 2021 17:20 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077 "2021-01-13T17:20:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [13 January 2021 17:20 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/1 "2021-01-13T17:20:15Z")

</div>

How do I add an object to a payload?

Here is what I have

```auto
[{"id":"d221c2c7.dcf32","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d92f539e.01a78","type":"inject","z":"d221c2c7.dcf32","name":"create fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"Bathroom\"}]","payloadType":"json","x":180,"y":80,"wires":[["385c74a6.71e1fc"]]},{"id":"7b7b8ba6.0deb14","type":"ui_table","z":"d221c2c7.dcf32","group":"37b5f1bd.773eae","name":"","order":1,"width":"6","height":"4","columns":[],"outputs":1,"cts":true,"x":780,"y":220,"wires":[[]]},{"id":"12b4a833.ab7ef8","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":140,"wires":[["385c74a6.71e1fc"]]},{"id":"9139f5f2.5c8b08","type":"inject","z":"d221c2c7.dcf32","name":"create fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"kitchen\"}]","payloadType":"json","x":180,"y":300,"wires":[["e22fb67e.179248"]]},{"id":"5475d1b0.9d254","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":360,"wires":[["e22fb67e.179248"]]},{"id":"88a6e89c.4cb5f8","type":"function","z":"d221c2c7.dcf32","name":"add to schedule","func":"var x;\nvar msg1;\n\nmsg1 = {payload: flow.get(\"fault1\")};\nmsg1.payload[1] = flow.get(\"fault2\");\n\nreturn msg1; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":220,"wires":[["7b7b8ba6.0deb14","c21a6a95.b5d448"]]},{"id":"e22fb67e.179248","type":"change","z":"d221c2c7.dcf32","name":"ui_control","rules":[{"t":"set","p":"fault2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":320,"wires":[["88a6e89c.4cb5f8"]]},{"id":"385c74a6.71e1fc","type":"change","z":"d221c2c7.dcf32","name":"ui_control","rules":[{"t":"set","p":"fault1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":120,"wires":[["88a6e89c.4cb5f8"]]},{"id":"c21a6a95.b5d448","type":"debug","z":"d221c2c7.dcf32","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":140,"wires":[]},{"id":"37b5f1bd.773eae","type":"ui_group","name":"1750+ 1 alarms","tab":"78efbcf6.3781f4","order":2,"disp":true,"width":"6","collapse":false},{"id":"78efbcf6.3781f4","type":"ui_tab","name":"Scheduler","icon":"autorenew","order":2,"disabled":false,"hidden":false}]

```

Here is the result

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bcb10793bc1ae5759bb9e2742fc867d8d2a6f706.png)

As you can see, it added the object, but as an array. I need them both to be objects (like [0]). How do I do this?

---

<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 2021 18:31 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/2 "2021-01-13T18:31:06Z")

</div>

Ok you add the first fault to `msg1.payload`, the first is an array containing an object.

So when you add the next fault which is an array, you then add it to msg1.payload[1]. So you put an array in an array.

what you need to do is  
`msg1 = {payload: flow.get("fault1")}; msg1.payload[1] = flow.get("fault2")[0];`

But without knowing what you are attempting to do there is not much more help we can give.

---

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [13 January 2021 19:28 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/3 "2021-01-13T19:28:16Z")

</div>

Thanks! Almost there.  
So what I'm trying to do is create a table of alarms. Node red will monitor a number of booleans. If a certain boolean goes true, then an alarm happened and a fault payload will be sent. What I have created simulates 2 booleans.

```auto
[{"id":"d221c2c7.dcf32","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d92f539e.01a78","type":"inject","z":"d221c2c7.dcf32","name":"create fault1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"Bathroom\"}]","payloadType":"json","x":190,"y":80,"wires":[["385c74a6.71e1fc"]]},{"id":"7b7b8ba6.0deb14","type":"ui_table","z":"d221c2c7.dcf32","group":"37b5f1bd.773eae","name":"","order":1,"width":"6","height":"4","columns":[],"outputs":1,"cts":true,"x":790,"y":220,"wires":[[]]},{"id":"12b4a833.ab7ef8","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":140,"wires":[["385c74a6.71e1fc"]]},{"id":"9139f5f2.5c8b08","type":"inject","z":"d221c2c7.dcf32","name":"create fault2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"kitchen\"}]","payloadType":"json","x":190,"y":300,"wires":[["e22fb67e.179248"]]},{"id":"5475d1b0.9d254","type":"inject","z":"d221c2c7.dcf32","name":"no fault","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"\",\"fault\":\"\"}]","payloadType":"json","x":170,"y":360,"wires":[["e22fb67e.179248"]]},{"id":"88a6e89c.4cb5f8","type":"function","z":"d221c2c7.dcf32","name":"add to schedule","func":"var x;\nvar msg1;\n\n\n\nmsg1 = {payload: flow.get(\"fault1\")}; msg.payload[1] = flow.get(\"fault2\")[0];\n//msg1.payload[1] = flow.get(\"fault2\");\n\nreturn msg; \n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":200,"wires":[["7b7b8ba6.0deb14","c21a6a95.b5d448"]]},{"id":"e22fb67e.179248","type":"change","z":"d221c2c7.dcf32","name":"construct alarm 2 payload","rules":[{"t":"set","p":"fault2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":320,"wires":[["88a6e89c.4cb5f8"]]},{"id":"385c74a6.71e1fc","type":"change","z":"d221c2c7.dcf32","name":"construct alarm 1 payload","rules":[{"t":"set","p":"fault1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":120,"wires":[["88a6e89c.4cb5f8"]]},{"id":"c21a6a95.b5d448","type":"debug","z":"d221c2c7.dcf32","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":140,"wires":[]},{"id":"a7bde91a.c0cc28","type":"comment","z":"d221c2c7.dcf32","name":"alarm 1","info":"","x":80,"y":120,"wires":[]},{"id":"bfb15a2.e8373a8","type":"comment","z":"d221c2c7.dcf32","name":"alarm 2","info":"","x":70,"y":340,"wires":[]},{"id":"7bdaf511.1ed95c","type":"comment","z":"d221c2c7.dcf32","name":"combines all messages","info":"prepare payload of alarms to deliver to table","x":620,"y":240,"wires":[]},{"id":"37b5f1bd.773eae","type":"ui_group","name":"1750+ 1 alarms","tab":"78efbcf6.3781f4","order":2,"disp":true,"width":"6","collapse":false},{"id":"78efbcf6.3781f4","type":"ui_tab","name":"Scheduler","icon":"autorenew","order":2,"disabled":false,"hidden":false}]

```

I added comments and names to the nodes. I hope it's easier to read now.

For some reason when I press create fault 2 that message gets written twice (once where fault 1 should be, and once where fault 2 should be.)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b32504fa5f42b4905e18d6b85cdcfe279131131d.png)

Can you explain what you have here?

```auto
msg1 = {payload: flow.get("fault1")}; msg.payload[1] = flow.get("fault2")[0];

```

Thanks!

---

<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 2021 19:34 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/4 "2021-01-13T19:34:21Z")

</div>

> [@traderjoe](#):
>
> `payload: flow.get("fault1")}; msg.payload[1] = flow.`

You wrote that not me, mine references `msg1.payload`, while what you typed is `msg.payload`.

But wht mine did with `flow.get("fault2")[0]`, was to target the object in fault2 not the array.

---

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [13 January 2021 19:57 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/5 "2021-01-13T19:57:52Z")

</div>

Oh I get it!  
So my payload is looking something like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23e52b67322c2ca8718d8ae25baa33eba6898b0b.png)

The problem is it's creating situations like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a95c2b6c9dd1682e51f4eeeb3fe3ecdf24868af.png)

It's because of the blanks in object 0. So I think the best way to do this is create the payload continuously and only add payloads when there is a fault. Otherwise don't add that alarm payload. How do I do that?

---

<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 2021 20:12 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/6 "2021-01-13T20:12:26Z")

</div>

You would need to add if statements to check values of objects and only add if the do not equal "". Or a switch node prior to the function to check if the property is not empty.

---

<div class="post-metadata">

**Author:** ![traderjoe](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@traderjoe](https://discourse.nodered.org/u/traderjoe)\
**Post date:** [13 January 2021 20:16 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/7 "2021-01-13T20:16:27Z")

</div>

Got it. Going back to your code:

```auto
msg1 = {payload: flow.get("fault1")}; msg1.payload[1] = flow.get("fault2")[0];

```

So let me paraphrase here and please correct where I'm wrong...

```auto
payload: flow.get("fault1")};

```

This gets creates the initial payload

```auto
msg1.payload[1] = flow.get("fault2")

```

This adds creates/add to object 1

what does the [0] at the end do?

---

<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 2021 22:08 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/8 "2021-01-13T22:08:47Z")

</div>

`flow.get("fault2")` is an array [{"fault":"kitchen"}]  
`flow.get("fault2")[0]` is the object {"fault":"kitchen"}. i.e position 0 of the array

But what you want to do is this, to add more increase number of messages in join node. At start up it has to receive all meassages, then after that any single message change will update table.

```auto
[{"id":"7015004b.e05608","type":"inject","z":"bc569d1f.364ac8","name":"no fault 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"null\",\"fault\":null}]","payloadType":"json","x":150,"y":140,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"4a7a4ae2.c9e9f4","type":"join","z":"bc569d1f.364ac8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":380,"y":200,"wires":[["a3ad66c0.31c058"]]},{"id":"e38a06c9.d6f2a","type":"inject","z":"bc569d1f.364ac8","name":"no fault 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"null\",\"fault\":null}]","payloadType":"json","x":150,"y":220,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"3cc29ec9.5d594a","type":"inject","z":"bc569d1f.364ac8","name":"create fault2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm2","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"kitchen\"}]","payloadType":"json","x":160,"y":260,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"501a4f71.9aea18","type":"inject","z":"bc569d1f.364ac8","name":"create fault1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"alarm1","payload":"[{\"timestamp\":\"1610488691209\",\"fault\":\"Bathroom\"}]","payloadType":"json","x":160,"y":180,"wires":[["4a7a4ae2.c9e9f4"]]},{"id":"a3ad66c0.31c058","type":"change","z":"bc569d1f.364ac8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[payload.*[*][fault != null]]\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":260,"wires":[["f7c7fdd4.5da26"]]},{"id":"f7c7fdd4.5da26","type":"debug","z":"bc569d1f.364ac8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":200,"wires":[]}]

```

---

<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 2021 22:08 UTC](https://discourse.nodered.org/t/add-an-object-to-a-payload/39077/9 "2021-03-14T22:08:50Z")

</div>

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