# Global Var in JSON

**URL:** <https://discourse.nodered.org/t/global-var-in-json/85286>\
**Category:** General\
**Tags:** javascript\
**Created:** [7 February 2024 09:32 UTC](https://discourse.nodered.org/t/global-var-in-json/85286 "2024-02-07T09:32:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerand](https://avatars.discourse-cdn.com/v4/letter/g/45deac/32.png) [@gerand](https://discourse.nodered.org/u/gerand)\
**Post date:** [7 February 2024 09:32 UTC](https://discourse.nodered.org/t/global-var-in-json/85286/1 "2024-02-07T09:32:54Z")

</div>

Hello,  
I would like to replace the injection nodes with a slider. To do this, I created a global variable “Slider”. How can I replace this with the fixed number 50 in JSON?

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/16fc50bae4b4c9443208c928df4291c693dddcf9.png)

I wanted to change this:

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51a2d3ba47819def44eb93caf9f33b121216ca23.png)

to this:

{  
"brightness": global.get("Slider"),  
"turn": "on"  
}

But it doesn´t work

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 February 2024 09:36 UTC](https://discourse.nodered.org/t/global-var-in-json/85286/2 "2024-02-07T09:36:29Z")

</div>

Change from static JSON to "expression" which uses JSONata. Then you can use the $globalContext function. The rest of your JSON can stay the same.

---

<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:** [7 February 2024 09:42 UTC](https://discourse.nodered.org/t/global-var-in-json/85286/3 "2024-02-07T09:42:44Z")

</div>

Far easier and cleaner to simply add another row to the inject node IMO

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a78e8addcc63af62f8a05344647cae4eef2fb428.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 February 2024 10:19 UTC](https://discourse.nodered.org/t/global-var-in-json/85286/4 "2024-02-07T10:19:08Z")

</div>

You aren't setting a global context variable solely to get the output of your slider into json format for passing to the Shelly are you?  
If so, here is an alternative approach:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/945f2001c71bb21b74d0b64be1427f860e8bb46f.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0ceb78fffa7bf7f807353e695cb10a1e883b2ac6.png)

---

<div class="post-metadata">

**Author:** ![gerand](https://avatars.discourse-cdn.com/v4/letter/g/45deac/32.png) [@gerand](https://discourse.nodered.org/u/gerand)\
**Post date:** [7 February 2024 10:50 UTC](https://discourse.nodered.org/t/global-var-in-json/85286/5 "2024-02-07T10:50:01Z")

</div>

Thanks for the quick responses! All 3 solutions work. In this use case I really just want to control the Shelly, but I also wanted to know how to assign global variables variably. Thank you very much!

---

<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:** [21 February 2024 10:50 UTC](https://discourse.nodered.org/t/global-var-in-json/85286/6 "2024-02-21T10:50:01Z")

</div>

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