# Set payload message with function node

**URL:** <https://discourse.nodered.org/t/set-payload-message-with-function-node/38551>\
**Category:** General\
**Created:** [4 January 2021 21:44 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551 "2021-01-04T21:44:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobbybau91](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobbybau91/32/34729_2.png) [@Bobbybau91](https://discourse.nodered.org/u/Bobbybau91)\
**Post date:** [4 January 2021 21:44 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551/1 "2021-01-04T21:44:05Z")

</div>

Hello,

first, sorry for my bad English and for my noob questions (I just started with node red). I have data I want to save in a .csv file. The problem ist, that the data quantity changes, so I don't need some time 3 data blocks of my input buffer and sometimes I need 10 data blocks of my input buffer.

To solve that I tried to make a small function, where I puzzle me the payload message as string. But it doesn't work.

Do anybody know how I can solve that problem?  
attached the debug of the bad payload and one of a good payload.

Best regards,  
Bob

 ![Bildschirmfoto 2021-01-04 um 22.39.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/39bb31941723c30aaec448c737d64bc97b54264e.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 January 2021 21:48 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551/2 "2021-01-04T21:48:49Z")

</div>

Hi and welcome to the forum.

It is good to see you are trying.

I am not going to be able to answer the question as I am not good with that sort of thing, but it would seem you are going through a problem I once went through.

Line 4 and 12.  
Delete them and put a `JSON` node after the `function` node.

That may fix the problem.

---

<div class="post-metadata">

**Author:** ![Bobbybau91](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobbybau91/32/34729_2.png) [@Bobbybau91](https://discourse.nodered.org/u/Bobbybau91)\
**Post date:** [4 January 2021 22:09 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551/3 "2021-01-04T22:09:23Z")

</div>

Hey

thank you for the answer, but it doesn't work.

Best regards,  
Bob

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 January 2021 22:11 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551/4 "2021-01-04T22:11:07Z")

</div>

Yeah, sorry. Open the `function` node, copy the code and paste it here.

It is not that easy reading it in the screen shot.

Use the `</>` button to enter the code.  
(Or: press the `key (one to the left of the`1`) THREE times, press enter, paste the code, press enter and press the key another three times)

Oh and post what you want the message to look like at the end.

---

<div class="post-metadata">

**Author:** ![Bobbybau91](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobbybau91/32/34729_2.png) [@Bobbybau91](https://discourse.nodered.org/u/Bobbybau91)\
**Post date:** [4 January 2021 22:29 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551/5 "2021-01-04T22:29:15Z")

</div>

I have the solution:

```auto
var i = 0
var csvDaten = {} 
var csvDatenObj = ""

for (i ; i < global.get("S7Input.iAnzahlWerte"); i++) {
    csvDatenObj = global.get("S7Input.sTitel"+i.toString());
    csvDaten[csvDatenObj] = global.get("S7Input.rWert"+i.toString());
}

msg.payload = csvDaten;

return msg;

```

Thanks for the help.  
 ![Bildschirmfoto 2021-01-04 um 23.30.04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bbca5b881485954279322fd26869433fc660a6ec.png)

---

<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:** [18 January 2021 22:29 UTC](https://discourse.nodered.org/t/set-payload-message-with-function-node/38551/6 "2021-01-18T22:29:45Z")

</div>

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