# I need help to add an array to an object

**URL:** <https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297>\
**Category:** General\
**Created:** [8 March 2023 14:04 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297 "2023-03-08T14:04:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackops](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@blackops](https://discourse.nodered.org/u/blackops)\
**Post date:** [8 March 2023 14:04 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/1 "2023-03-08T14:04:10Z")

</div>

Hello i hope somebody can help me.  
My Payload looks now like that  
 ![Now](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/334c4cd6379c714130381fef5fc89820db18b8cd.png)  
I need to change it like that  
 ![need](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/dd61be6d2171517e06ed2ed532a21946dcf73ae3.png)

---

<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:** [8 March 2023 14:16 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/2 "2023-03-08T14:16:28Z")

</div>

how do they correspond as the numbers are both different in both images, what goes where.  
Also supply your input data a a copyable text. and also how the output should be using the input data values, so numbers correspond and we can see what needs moving where.

---

<div class="post-metadata">

**Author:** ![blackops](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@blackops](https://discourse.nodered.org/u/blackops)\
**Post date:** [8 March 2023 14:24 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/3 "2023-03-08T14:24:10Z")

</div>

Now it output this  
{"canid":416,"a":0,"b":64,"c":178,"d":21,"e":254,"f":254,"g":0,"h":16}  
I need it like that  
{"canfd":false,"ext":false,"rtr":false,"canid":416,"dlc":5,"data":[0,64,178,21,254,254,0,16]}

---

<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:** [8 March 2023 14:34 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/4 "2023-03-08T14:34:18Z")

</div>

You can use a change node and JSONata  
e.g.

```auto
[{"id":"21a060ef7df935b9","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"canid\":416,\"0\":0,\"1\":64,\"2\":178,\"3\":21,\"4\":254,\"5\":254,\"6\":0,\"7\":16}","payloadType":"json","x":110,"y":220,"wires":[["22fb581c516a0fc7"]]},{"id":"22fb581c516a0fc7","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"canfd\":false,\t \"ext\":false,\t \"rtr\":false,\t \"canid\":$$.payload.canid,\t \"dlc\":5,\t \"data\":($$.payload ~> |$|{}, \"canid\"|).*\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":200,"wires":[["30d55c3ac12c7bfd"]]},{"id":"30d55c3ac12c7bfd","type":"debug","z":"65617ffeb779f51c","name":"debug 239","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":180,"wires":[]}]

```

expression

```auto
{
  "canfd":false,
  "ext":false,
  "rtr":false,
  "canid":$$.payload.canid,
  "dlc":5,
  "data":($$.payload ~> |$|{}, "canid"|).*
}

```

or use a template node that will guarantee the array positions. e.g.

```auto
[{"id":"21a060ef7df935b9","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"canid\":416,\"a\":0,\"b\":64,\"c\":178,\"d\":21,\"e\":254,\"f\":254,\"g\":0,\"h\":16}","payloadType":"json","x":91,"y":244.00000667572021,"wires":[["5655b765ce604c35"]]},{"id":"5655b765ce604c35","type":"template","z":"65617ffeb779f51c","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n \"canfd\":false,\n \"ext\":false,\n \"rtr\":false,\n \"canid\": {{payload.canid}},\n \"dlc\":5,\n \"data\": [\n {{payload.a}},\n {{payload.b}},\n {{payload.c}},\n {{payload.d}},\n {{payload.e}},\n {{payload.f}},\n {{payload.g}},\n {{payload.h}}\n]\n}","output":"json","x":301,"y":244.00000667572021,"wires":[["30d55c3ac12c7bfd"]]},{"id":"30d55c3ac12c7bfd","type":"debug","z":"65617ffeb779f51c","name":"debug 239","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":451,"y":184.00000667572021,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Ithoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ithoh/32/40000_2.png) [@Ithoh](https://discourse.nodered.org/u/Ithoh)\
**Post date:** [8 March 2023 14:41 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/5 "2023-03-08T14:41:33Z")

</div>

Looks like you are working with CAN frames & CAN buses, assuming your input is comming from a CAN bus, you could try using the : node-red-contrib-socketcan node, which read from the CAN buses and output the data the way you need it to be

---

<div class="post-metadata">

**Author:** ![blackops](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@blackops](https://discourse.nodered.org/u/blackops)\
**Post date:** [9 March 2023 12:18 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/6 "2023-03-09T12:18:14Z")

</div>

thank you very much it works ❤

---

<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:** [23 March 2023 12:18 UTC](https://discourse.nodered.org/t/i-need-help-to-add-an-array-to-an-object/76297/7 "2023-03-23T12:18:56Z")

</div>

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