# How can I declare a msg.payload variable inside a json node no make a pdf?

**URL:** <https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385>\
**Category:** Dashboard\
**Tags:** node-red-dashboard\
**Created:** [18 December 2022 16:26 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385 "2022-12-18T16:26:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo1](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eduardo1](https://discourse.nodered.org/u/Eduardo1)\
**Post date:** [18 December 2022 16:26 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/1 "2022-12-18T16:26:28Z")

</div>

I used a node join to create an array of payloads. Now I need to declare different variables inside a button node using the json format to insert these variables into a pdf (node-red-contrib-pdfmake)  
How can I do this?

---

<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:** [18 December 2022 16:54 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/2 "2022-12-18T16:54:04Z")

</div>

> [@Eduardo1](#):
>
> I need to declare different variables inside a button node using the json format

Sorry that doesn't convey any useful information to me. Can you describe in more detail what you want to get out of the button node? Tell us the structure of the data exactly.

In the title you say json node, did you mean button node?

---

<div class="post-metadata">

**Author:** ![Eduardo1](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eduardo1](https://discourse.nodered.org/u/Eduardo1)\
**Post date:** [18 December 2022 17:13 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/3 "2022-12-18T17:13:52Z")

</div>

![node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2c52641bd78d664707435104cc41ecbbdc267708.png)  
The node "gerar relatorio" is my json inject and "join" is my node where I created the payload array.  
"Gerar relatorio" is an dashboar button (node-red-contrib-dashboard)

---

<div class="post-metadata">

**Author:** ![Eduardo1](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eduardo1](https://discourse.nodered.org/u/Eduardo1)\
**Post date:** [18 December 2022 17:18 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/4 "2022-12-18T17:18:08Z")

</div>

and I need to get all msg.payloads that I created inside the "join", and use it inside my json inject. I have to declare variables to each payload in json format

---

<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:** [18 December 2022 17:26 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/5 "2022-12-18T17:26:58Z")

</div>

I don't think you can get the button to remember data you send it and then pass it on when clicked. Instead of doing that, feed the button output into the Join node, which I presume you have got in key/value pair mode, and you can check the output of the join for the button topic and only pass that on to the nodes to build your pdf.

---

<div class="post-metadata">

**Author:** ![Eduardo1](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eduardo1](https://discourse.nodered.org/u/Eduardo1)\
**Post date:** [18 December 2022 17:30 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/6 "2022-12-18T17:30:07Z")

</div>

![noden](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a3704e6ed770edff03fb0f1b172967341e05e3d.png)  
Like that?

---

<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:** [18 December 2022 17:35 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/7 "2022-12-18T17:35:01Z")

</div>

Yes, but with a Switch node after the Join which passes the message only when the button topic is received. The Join node must be in key/value pair mode, with And every subsequent message selected. I don't know whether you are using key/value mode already.

If not then change to that mode, if you can't then you will need to add another Join node after the first one, with the button fed into that, to join the output from the first with the button press.

---

<div class="post-metadata">

**Author:** ![Eduardo1](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eduardo1](https://discourse.nodered.org/u/Eduardo1)\
**Post date:** [18 December 2022 17:40 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/8 "2022-12-18T17:40:23Z")

</div>

And which configuration I should do inside switch node?

---

<div class="post-metadata">

**Author:** ![Eduardo1](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eduardo1](https://discourse.nodered.org/u/Eduardo1)\
**Post date:** [18 December 2022 17:43 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/9 "2022-12-18T17:43:37Z")

</div>

![Screenshot_1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80ee06d45db1603e946f2273c8279357f554866d.png)

Edit: and anyway, my "pdfmake" need to receive the button payload in JSON format to convert into pdf.  
And all informations that I need to appear inside the PDF must have in JSON format

---

<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:** [18 December 2022 18:00 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/10 "2022-12-18T18:00:18Z")

</div>

> [@Eduardo1](#):
>
> which configuration I should do inside switch node?

I told you, so that it only passes messages with the topic set to that you set in the button, not the topic that you need to set in the other path. I presume you do understand how the join node works in that mode? If not then have a play with it so you understand.

If the data is not in the right format then adjust it as necessary after the switch.

---

<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:** [18 December 2022 18:31 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/11 "2022-12-18T18:31:11Z")

</div>

> [@Colin](#):
>
> I don't think you can get the button to remember data you send it and then pass it on when clicked.

It is possible but requires using msg.topic and some shuffling  
e.g.

```auto
[{"id":"98c1b87d53a3afbd","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"button","payload":"{\"one\":1}","payloadType":"json","x":140,"y":480,"wires":[["3854bc21929e8bed"]]},{"id":"3854bc21929e8bed","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"move","p":"topic","pt":"msg","to":"topic[0]","tot":"msg"},{"t":"move","p":"payload","pt":"msg","to":"topic[1]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":500,"wires":[["815e6dc3d2e697c2"]]},{"id":"5a567fdc71f90570","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"button","payload":"{\"two\":2}","payloadType":"json","x":140,"y":540,"wires":[["3854bc21929e8bed"]]},{"id":"815e6dc3d2e697c2","type":"ui_button","z":"da8a6ef0b3c9a5c8","name":"","group":"2d4fe667.28f8ba","order":17,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"{}","payloadType":"json","topic":"topic","topicType":"msg","x":490,"y":500,"wires":[["28f8aa05230185d5"]]},{"id":"28f8aa05230185d5","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"move","p":"topic[1]","pt":"msg","to":"payload","tot":"msg"},{"t":"move","p":"topic[0]","pt":"msg","to":"topic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":500,"wires":[["b3af9a4094965b6b"]]},{"id":"b3af9a4094965b6b","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 115","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":560,"wires":[]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":2,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

 ![chrome_screenshot_1671388115256](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68d2e18719e394dee6dd6c5d4c7f3e5ba211d70f.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:** [18 December 2022 21:45 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/12 "2022-12-18T21:45:17Z")

</div>

I like it. A bit of lateral thinking can go a long way.

@Eduardo1 that is a simpler solution than mine.

---

<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:** [17 January 2023 21:45 UTC](https://discourse.nodered.org/t/how-can-i-declare-a-msg-payload-variable-inside-a-json-node-no-make-a-pdf/72385/13 "2023-01-17T21:45:52Z")

</div>

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