I need help to add an array to an object

Hello i hope somebody can help me.
My Payload looks now like that
Now
I need to change it like that
need

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.

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]}

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

[{"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

{
  "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.

[{"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":[]}]
1 Like

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

thank you very much it works :heart:

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