# Global JSON variable

**URL:** <https://discourse.nodered.org/t/global-json-variable/36084>\
**Category:** General\
**Created:** [17 November 2020 18:37 UTC](https://discourse.nodered.org/t/global-json-variable/36084 "2020-11-17T18:37:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lolimpol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lolimpol/32/32136_2.png) [@lolimpol](https://discourse.nodered.org/u/lolimpol)\
**Post date:** [17 November 2020 18:37 UTC](https://discourse.nodered.org/t/global-json-variable/36084/1 "2020-11-17T18:37:32Z")

</div>

Hi, I'm pretty new to Node-red, I've been using it for about 2 years, but only to set things up. Im doing a mayor overhaull on my MQTT based smarthome, which has lights with many ways of controlling them, and I want my dashboard to display all the data changed somewhere else. I decided to use a global variable, since I can just send out a JSON object straight from the global var to MQTT. so I want to have a global JSON object, like this:  
payload{  
brightness:0-255  
white\_value:0-255  
color{  
r:0-255  
g:0-255  
b:0-255  
}  
transition:0-150  
effect:string  
state:string  
}

now if I for example change the value of brightness, I want to change that in the object (in a function node) but keep the rest 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:** [17 November 2020 18:44 UTC](https://discourse.nodered.org/t/global-json-variable/36084/2 "2020-11-17T18:44:11Z")

</div>

> [@lolimpol](#):
>
> now if I for example change the value of brightness, I want to change that in the object (in a function node) but keep the rest the same

you can retrieve the JS object from global context using `global.get("name-of-object")` and change 1 thing e.g...

```auto
var myObject = global.get("name-of-object") || {};
myObject.brightness = 123;
msg.payload = myObject;
return msg

```

#### Important note the above function ^ WILL change the object stored in the global store (since it is a reference)

PS, "JSON object" is not really a thing.. JSON mans "JavaScript Object Notation" - i.e. it is a String representation of a JS object.

---

<div class="post-metadata">

**Author:** ![lolimpol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lolimpol/32/32136_2.png) [@lolimpol](https://discourse.nodered.org/u/lolimpol)\
**Post date:** [17 November 2020 18:46 UTC](https://discourse.nodered.org/t/global-json-variable/36084/3 "2020-11-17T18:46:27Z")

</div>

I understand that, but in my case I want to use global.set() for just the value myObject.brightness, and not affect any other value in the Object. If I simply use global.set("myObject", brightness), the whole object will just become the brightness.

---

<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:** [17 November 2020 18:48 UTC](https://discourse.nodered.org/t/global-json-variable/36084/4 "2020-11-17T18:48:35Z")

</div>

> [@lolimpol](#):
>
> If I simply use global.set("myObject", brightness), the whole object will just become the brightness.

then dont do that. put the whole object back in the store

```auto
var myObject = global.get("name-of-object") || {};
myObject.brightness = 123;
global.set("name-of-object", myObject)

```

As i alluded to earlier, it isnt _really_ necessary (but is good practice) to call global.set BECAUSE the object it a reference & the globally stored object WILL be updated by simply setting `myObject.brightness = 123`

best thing you can do is give it a go - best way to learn 🙂

---

<div class="post-metadata">

**Author:** ![lolimpol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lolimpol/32/32136_2.png) [@lolimpol](https://discourse.nodered.org/u/lolimpol)\
**Post date:** [17 November 2020 18:51 UTC](https://discourse.nodered.org/t/global-json-variable/36084/5 "2020-11-17T18:51:06Z")

</div>

well all I make is shotty code, and since I'm too lazy to update all of the function nodes I have I'll just try the method stated below the proper way to do it, I'll never learn 😅

thanks a lot for the answer!

---

<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:** [17 November 2020 19:38 UTC](https://discourse.nodered.org/t/global-json-variable/36084/6 "2020-11-17T19:38:01Z")

</div>

You can also use dot notation in the string.  
`global.set('myObject.brightness', 255)`  
or set the `global myObject.brightness` in a change node.  
i.e.

```auto
[{"id":"a284d8c1.e2dd","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":590,"y":1760,"wires":[["dd06c3ac.4874a8"]]},{"id":"c815e9db.a393f8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"255","payloadType":"num","x":550,"y":1800,"wires":[["3be8855b.56d692"]]},{"id":"dd06c3ac.4874a8","type":"function","z":"8d22ae29.7df6d","name":"","func":"global.set(\"myObject.brightness\",msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":750,"y":1760,"wires":[[]]},{"id":"3be8855b.56d692","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"myObject.brightness","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":1800,"wires":[[]]}]

```

---

<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 01:10 UTC](https://discourse.nodered.org/t/global-json-variable/36084/7 "2020-12-30T01:10:15Z")

</div>

I need to pull a global variable using JSON how do I do that  
global. payload.room = msg.payload.room and add the value of the button that was pressed

@Steve-Mcl @E1cid

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 December 2020 01:36 UTC](https://discourse.nodered.org/t/global-json-variable/36084/8 "2020-12-30T01:36:51Z")

</div>

@Zbrooklyn - please don't post the same question in multiple [threads](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/31). Please do as I said in the other thread - i.e. open a new thread and explain what you are doinf and what you have tried.

---

<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:** [28 February 2021 01:37 UTC](https://discourse.nodered.org/t/global-json-variable/36084/9 "2021-02-28T01:37:05Z")

</div>

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