# Change payload form

**URL:** <https://discourse.nodered.org/t/change-payload-form/76755>\
**Category:** General\
**Tags:** modbus\
**Created:** [20 March 2023 14:41 UTC](https://discourse.nodered.org/t/change-payload-form/76755 "2023-03-20T14:41:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sekuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sekuti/32/107159_2.png) [@Sekuti](https://discourse.nodered.org/u/Sekuti)\
**Post date:** [20 March 2023 14:41 UTC](https://discourse.nodered.org/t/change-payload-form/76755/1 "2023-03-20T14:41:44Z")

</div>

Hello,

I am trying to replace tags from s7 node with tags from modbus. In s7 tags are formed like this "{"I1":22.463211059570312,"I2":11.89554214477539,"I3":22.009639739990234,"L1":228.79904174804688,"L2":229.78207397460938,"L3":228.8221435546875,"Frequency":50.022239685058594,"I\_avarage":18.789464950561523,"F1":0.969420850276947,"F2":0.8568284511566162,"F3":0.9685365557670593}"

and out of modbus I get all this  
"{"payload":21.9,"topic":"currentL3","format":"float32","unit":"A","model":"pac3220","\_msgid":"3385c039ec7c7315","messageId":"64186ee1e446920650695b20","modbusRequest":{"unitid":1,"fc":3,"address":17,"quantity":2,"emptyMsgOnFail":false,"keepMsgProperties":true,"messageId":"64186ee1e446920650695b20"},"values":[16815,11993]}"

So how can I transform payload from modbus "getter" to get just for example "{"currentL3" = 21.9}"

br

Aleksander

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 March 2023 19:35 UTC](https://discourse.nodered.org/t/change-payload-form/76755/2 "2023-03-20T19:35:45Z")

</div>

> [@Sekuti](#):
>
> "{"payload":21.9,"topic":"currentL3",

Use a Change node with an expression

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

```auto
[{"id":"01be8de27b6ec3b1","type":"inject","z":"54efb553244c241f","name":"modbus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"currentL3","payload":"21.9","payloadType":"num","x":310,"y":2520,"wires":[["9bc3391967f54dbe"]]},{"id":"425e438b793a5470","type":"debug","z":"54efb553244c241f","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":2520,"wires":[]},{"id":"9bc3391967f54dbe","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ topic: payload }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2520,"wires":[["425e438b793a5470"]]}]

```

---

<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:** [20 March 2023 20:16 UTC](https://discourse.nodered.org/t/change-payload-form/76755/3 "2023-03-20T20:16:16Z")

</div>

Or without using any code.

 ![chrome_screenshot_1679343310477](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca443b0ae38e7e88aa322e067ccb112a875b0fb2.png)

```auto
[{"id":"01be8de27b6ec3b1","type":"inject","z":"65617ffeb779f51c","name":"modbus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"currentL3","payload":"21.9","payloadType":"num","x":920.0000228881836,"y":4122.000285148621,"wires":[["9bc3391967f54dbe"]]},{"id":"9bc3391967f54dbe","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload[msg.topic]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140.0000228881836,"y":4122.000285148621,"wires":[["425e438b793a5470"]]},{"id":"425e438b793a5470","type":"debug","z":"65617ffeb779f51c","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1370.0000228881836,"y":4122.000285148621,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Sekuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sekuti/32/107159_2.png) [@Sekuti](https://discourse.nodered.org/u/Sekuti)\
**Post date:** [21 March 2023 07:10 UTC](https://discourse.nodered.org/t/change-payload-form/76755/4 "2023-03-21T07:10:07Z")

</div>

... first thanks for reply. But I would like to erase all unnecessary infos from object. To get like on example (I:61.422...)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b0966515c8197e45d858e1b47f695e727da1b93.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [21 March 2023 07:28 UTC](https://discourse.nodered.org/t/change-payload-form/76755/5 "2023-03-21T07:28:31Z")

</div>

In that case you can use a Function node to create a new clean Object and use only the fields you need to be passed on.

```auto
let newMsg = { payload: { [msg.topic]: msg.payload } }
return newMsg;

```

Example

```auto
[{"id":"01be8de27b6ec3b1","type":"inject","z":"54efb553244c241f","name":"modbus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"currentL3","payload":"21.9","payloadType":"num","x":230,"y":2180,"wires":[["e093823c709e94ac"]]},{"id":"425e438b793a5470","type":"debug","z":"54efb553244c241f","name":"debug 36","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":2180,"wires":[]},{"id":"e093823c709e94ac","type":"function","z":"54efb553244c241f","name":"function","func":"let newMsg = { payload: { [msg.topic]: msg.payload } }\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":2180,"wires":[["425e438b793a5470"]]}]

```

ps. the reason you are seeing the full object in the top msg is probably because you have the Debug node set to show "complete msg object". you could set it to show only `msg.payload` and ignore the rest

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2023 07:52 UTC](https://discourse.nodered.org/t/change-payload-form/76755/6 "2023-03-21T07:52:40Z")

</div>

> [@Sekuti](#):
>
> I would like to erase all unnecessary infos from object.

Why do you want to do that?

---

<div class="post-metadata">

**Author:** ![Sekuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sekuti/32/107159_2.png) [@Sekuti](https://discourse.nodered.org/u/Sekuti)\
**Post date:** [21 March 2023 08:38 UTC](https://discourse.nodered.org/t/change-payload-form/76755/7 "2023-03-21T08:38:50Z")

</div>

... because I have all logic behind adopt to that type of data and now I would like to change acquisition type from S7 node to modbus node without changing anything else because system is tested and already in use.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2023 08:58 UTC](https://discourse.nodered.org/t/change-payload-form/76755/8 "2023-03-21T08:58:54Z")

</div>

When you said

> [@Sekuti](#):
>
> I would like to erase all unnecessary infos from object

What object did you mean? msg.payload or the complete message.

Additional properties in the message will not usually affect later nodes.

---

<div class="post-metadata">

**Author:** ![Sekuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sekuti/32/107159_2.png) [@Sekuti](https://discourse.nodered.org/u/Sekuti)\
**Post date:** [21 March 2023 09:12 UTC](https://discourse.nodered.org/t/change-payload-form/76755/9 "2023-03-21T09:12:08Z")

</div>

... I hope that additional properties will not affect. I adopt now payload with all infos and some more properties. I just want to have payload only with properties that I need but if that is not possible it is also OK as long as it works.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52eb3e2cfe1c889be0d63d4cfbae2a18482f0c27.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2023 09:36 UTC](https://discourse.nodered.org/t/change-payload-form/76755/10 "2023-03-21T09:36:51Z")

</div>

> [@Sekuti](#):
>
> I just want to have payload only with properties that I need but if that is not possible

Of course it is possible, @UnborN showed you how to do it, but there is no point adding the additional overhead of creating a new message unless there is some benefit.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 March 2023 10:16 UTC](https://discourse.nodered.org/t/change-payload-form/76755/11 "2023-03-21T10:16:41Z")

</div>

> [@Colin](#):
>
> unless there is some benefit.

... maybe clarity for the user to see exactly what is now in the msg ?

I know for you and I that it doesn't really matter (and is often preferable) to just modify the part of the msg we need later - but it can make debugging messier, and clarity when you come back to it in 6 months may be more useful.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [21 March 2023 18:34 UTC](https://discourse.nodered.org/t/change-payload-form/76755/12 "2023-03-21T18:34:45Z")

</div>

> [@Sekuti](#):
>
> I just want to have payload only with properties that I need

Just a word of caution, if I may. Messages sometimes have properties that we don't notice but are needed for the proper operation of a flow. For example, messages in a sequence produced by a `split` node have a `msg.parts` property that is used by downstream `join`, `sort`, and `batch` nodes. Also, if a message passes through a `link-out` node, it gets a msg.\_linkSource property that allows it to return to the calling flow. Various nodes use properties like `msg.target`, `msg.reset`, and others for specific purposes, so if any of these have been set before the message reaches your `function` node, there could be issues downstream. You might consider the balance between having a "clean" message structure in your current flow and creating bugs if you modify the flow or re-use it in new projects.

---

<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:** [20 May 2023 18:34 UTC](https://discourse.nodered.org/t/change-payload-form/76755/13 "2023-05-20T18:34:52Z")

</div>

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