# Pulling global values and and merging other variables using json

**URL:** <https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258>\
**Category:** General\
**Created:** [30 December 2020 05:05 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258 "2020-12-30T05:05:47Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [30 December 2020 05:05 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/1 "2020-12-30T05:05:48Z")

</div>

I need to pull a global variable using JSON how do I do that  
I was able to pull the global value like this

var ObtainedData = global.get("GlobalRoom");  
msg.payload = ObtainedData;  
return msg;

based on this video [https://www.youtube.com/watch?v=QDFONY4WOHw](https://www.youtube.com/watch?v=QDFONY4WOHw)  
but I don't like that way because it requires a few different nodes and does not fully give me what I need

I want with one button click using a trigger node or a dashboard button node using JSON to get the global value global.payload.room convert it to msg.payload.room and also add the value of the button that was pressed example msg.payload.button = button1

"global. payload.room = msg.payload.room and add the value of the button that was pressed"

 ![WhatsApp Image 2020-12-30 at 12.08.45 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f3b7ccd3c4319bab6fdb14d41bf062a456a472c.jpeg)

in this picture, you can see I have a dropdown that outputs global.payload.room  
and when I push a button I want the output to be the value of the dropdown and the button in one message

msg.topic= button pressed  
msg.payload.room  
msg.payload.button

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [30 December 2020 07:40 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/2 "2020-12-30T07:40:26Z")

</div>

Share your flow, this will make it more likely that you will get help with a solution. In principle you will have to catch the button press message and combine that with a global variable. This will require that the "button" part or component has access to what has been selected from the drop down. WIthout knowing what and how you have done, well impossible to give advice

If the web page above is fully written in a template node using html/javascript you could combine them there. If you have used NR dashboard nodes you have to catch messages and combine them differently

---

<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:** [30 December 2020 08:24 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/3 "2020-12-30T08:24:01Z")

</div>

Not sure of your global name as it appears to change in your post.  
And not sure what you mean by JSON

But here is my guess on what you may require.

```auto
[{"id":"337cc23.e8022be","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":2340,"wires":[["7d1d4dc1.101b1c"]]},{"id":"7d1d4dc1.101b1c","type":"ui_button","z":"8d22ae29.7df6d","name":"","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"passthru":true,"label":"button 1","tooltip":"","color":"","bgcolor":"","icon":"","payload":"button 1","payloadType":"str","topic":"","x":250,"y":2300,"wires":[["efb018ff.1c1b7"]]},{"id":"ffd491fb.4123b8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":2220,"wires":[]},{"id":"efb018ff.1c1b7","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.button","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"payload.button","tot":"msg"},{"t":"set","p":"payload.room","pt":"msg","to":"$globalContext(\"GlobalRoom\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":2340,"wires":[["ffd491fb.4123b8"]]},{"id":"165e1e1a.e752fa","type":"ui_group","z":"","name":"Default","tab":"f9b58883.fa613","order":1,"disp":true,"width":"12","collapse":false},{"id":"f9b58883.fa613","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

Will need more info if this is not right.  
probably best to export t a section of your flow that you would like shorten, and a example of the global.

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [30 December 2020 19:39 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/4 "2020-12-30T19:39:03Z")

</div>

this is where I plan to put the code

 ![Screenshot 2020-12-30 144119](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d74c5eb3293c94d3dc7d63ecb117fe78a46fef3.png)

I grouped the nodes so you can see what I am trying to do

[https://creditkey.mybluemix.net/red/#flow/23cf197d.f319c6](https://creditkey.mybluemix.net/red/#flow/23cf197d.f319c6)

 ![Screenshot 2020-12-30 143729](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b26fb03cf99eb0a67072df8a6c1e1204ba8694bb.png)

for the payload, i want to use the built-in script function witch uses JSON code

 ![Screenshot 2020-12-30 143705](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8544445a1db5aba874c7d91874dfb66ae326f87.png)

@krambriw @E1cid

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [30 December 2020 19:52 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/5 "2020-12-30T19:52:25Z")

</div>

> [@Zbrooklyn](#):
>
> msg.topic= button pressed  
> msg.payload.room  
> msg.payload.button

these are the payload variables

the dropdown is  
global.payload.room

so i want the button  
to pull global.payload.room, convert to msg.payload.room (the output value is 1,2,3,4,5,6)

the button value should go under msg.payload.button = "button1"

and the message topic should be msg.topic= "button pressed"

---

<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:** [30 December 2020 20:36 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/6 "2020-12-30T20:36:00Z")

</div>

If i understand, it is not possible to select JSON { } in button and access a global. It would be possible if the button node had access to JSONata library.

Only way is to set the global in the button and a topic then use change node to move items and buttn value., as below

```auto
[{"id":"240a26c1.496232","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":2460,"wires":[["bb5afb61.3ebe"]]},{"id":"bb5afb61.3ebe","type":"ui_button","z":"8d22ae29.7df6d","name":"","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"passthru":true,"label":"1","tooltip":"","color":"","bgcolor":"","icon":"","payload":"GlobalRoom","payloadType":"global","topic":"button1","x":240,"y":2460,"wires":[["f08bf02b.ca85c8"]]},{"id":"f08bf02b.ca85c8","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.room","tot":"msg"},{"t":"set","p":"payload.button","pt":"msg","to":"$substring(topic, -1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":2480,"wires":[["b7b4d5d5.c27648"]]},{"id":"8d70060b.ac9808","type":"ui_button","z":"8d22ae29.7df6d","name":"","group":"165e1e1a.e752fa","order":7,"width":0,"height":0,"passthru":true,"label":"2","tooltip":"","color":"","bgcolor":"","icon":"","payload":"GlobalRoom","payloadType":"global","topic":"button2","x":230,"y":2500,"wires":[["f08bf02b.ca85c8"]]},{"id":"b7b4d5d5.c27648","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":2440,"wires":[]},{"id":"3ccef7dc.ffadf8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":2500,"wires":[["8d70060b.ac9808"]]},{"id":"7691e4a9.e46244","type":"ui_dropdown","z":"8d22ae29.7df6d","name":"","label":"","tooltip":"","place":"Select option","group":"165e1e1a.e752fa","order":8,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"room 1","value":"room 1","type":"str"},{"label":"room 2","value":"room 2","type":"str"}],"payload":"","topic":"","x":260,"y":2400,"wires":[["b7b4d5d5.c27648","6c7bc84b.a6499"]]},{"id":"6799842e.87b524","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"room 1","payloadType":"str","x":80,"y":2400,"wires":[["7691e4a9.e46244"]]},{"id":"6c7bc84b.a6499","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"GlobalRoom","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2400,"wires":[[]]},{"id":"165e1e1a.e752fa","type":"ui_group","z":"","name":"Default","tab":"f9b58883.fa613","order":1,"disp":true,"width":"12","collapse":false},{"id":"f9b58883.fa613","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [30 December 2020 23:03 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/7 "2020-12-30T23:03:31Z")

</div>

> [@E1cid](#):
>
> JSONata library.

it does have access look at the pitures i posted

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [30 December 2020 23:28 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/8 "2020-12-30T23:28:08Z")

</div>

> [@Zbrooklyn](#):
>
> for the payload, i want to use the built-in script function witch uses JSON code

The JSON type does not allow any sort of dynamic construction of the value - it's for fixed values only. The screenshot you've shared shows the JSON editor where you enter the fixed value. It cannot reference any global or msg values.

When a button is clicked you'll get a message with `msg.payload` set to a number. From the screenshot of the button config you've shared, it looks like it will send the value `1`.

To get the message you want, you could use a Function node with the code:

```auto
msg.topic = "button pressed";
msg.payload = {
   room: global.get("payload.room"),
   button: msg.payload
}
return msg;

```

Or you could use a Change node with the following rules:

1. `move msg.payload to msg.button`
2. `set msg.payload to (JSON) {}`
3. `set msg.payload.room to global.payload.room`
4. `move msg.button to msg.payload.button`
5. `set msg.topic to "button pressed"`

Note the need to move the existing `msg.payload` to a different property temporarily so that you can construct `msg.payload` as the object you want.

A different way with the Change node would be the rules:

1. `set msg.topic to "button pressed"`
2. `set msg.payload to (Expression) { "room": $globalContext("payload.room"), "button": $.payload}`

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [30 December 2020 23:48 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/9 "2020-12-30T23:48:40Z")

</div>

ok i will try the 3 methods

---

<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:** [31 December 2020 01:10 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/10 "2020-12-31T01:10:44Z")

</div>

Your picture shows a JSON input, not a JSONata expression input.  
Did you even try my example?

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [31 December 2020 07:43 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/11 "2020-12-31T07:43:55Z")

</div>

> [@Zbrooklyn](#):
>
> payload.room

what is the best way to set my dropdown to a global value?

I don't think it is set up right for a global value  
because the options you gave me I'm not seeing the global value in the debug node

![Screenshot 2020-12-31 024012](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3f5499490b2900cd7b37066951e0df4d31466f3.png)

you can check out the flow I updated it and grouped it better

 ![Screenshot 2020-12-31 023031](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e846e4f7f63e45e09f276e333c6153d8aa5eb37a.png)

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [31 December 2020 07:45 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/12 "2020-12-31T07:45:39Z")

</div>

i think i got it

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [31 December 2020 08:07 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/13 "2020-12-31T08:07:55Z")

</div>

now i need to join payload.room and payload.button like this 1.1 first "1" is the room and second 1 is the button value

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2020 08:12 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/14 "2020-12-31T08:12:18Z")

</div>

use a jsonata expression or function to concatenate the values

e.g. in a function

```auto
msg.payload.roomButton = msg.payload.room + "." + msg.payload.button;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [31 December 2020 08:34 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/15 "2020-12-31T08:34:18Z")

</div>

thank you that worked I think I finally got it

tomorrow I can explain what this project is for  
i think my project can be a great sorce

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [1 January 2021 20:27 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/16 "2021-01-01T20:27:08Z")

</div>

@E1cid @knolleary @krambriw please check out my latest topic

> [@Creating a custom dropdown using html](https://discourse.nodered.org/t/creating-a-custom-dropdown-using-html/38370):
>
> I created a custom HTML custom button using a template mode it works great now I want to do the same thing but with a dropdown selection here s the HTML code I used for the button \<style\> :root { --myHighlight:url(''); } .filled { white-space:normal; height: 100% !important; width: 100% !important; padding: 0 !important; margin: 0px !important; //background-color: transparent !important; } .nr-dashboard-template { padding: 0; …

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 January 2021 20:39 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/17 "2021-01-01T20:39:55Z")

</div>

@Zbrooklyn please do not tag us into your other threads. There is no need to do that. We will see your posts just like every other post.

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [2 January 2021 23:34 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/18 "2021-01-02T23:34:45Z")

</div>

ok sorry still getting used to the forum

---

<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:** [3 March 2021 23:34 UTC](https://discourse.nodered.org/t/pulling-global-values-and-and-merging-other-variables-using-json/38258/19 "2021-03-03T23:34:53Z")

</div>

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