# JSON global variables in the CHANGE node

**URL:** <https://discourse.nodered.org/t/json-global-variables-in-the-change-node/74216>\
**Category:** General\
**Tags:** function-node\
**Created:** [26 January 2023 14:35 UTC](https://discourse.nodered.org/t/json-global-variables-in-the-change-node/74216 "2023-01-26T14:35:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [26 January 2023 14:35 UTC](https://discourse.nodered.org/t/json-global-variables-in-the-change-node/74216/1 "2023-01-26T14:35:22Z")

</div>

Is there a smarter way to prepare this JSON?

I would like all this to be in a single node CHANGE ?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/811e49a8e37f564c9173445afd272533ef86c25a.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/244940d8f80755c11a25dba7f95ea0eeef9f2e55.png)

```auto
[{"id":"effa65099dd2a525","type":"function","z":"ea86445f05051d5d","name":"prep_MQTT_Conn","func":"\nconst ip = msg.payload.ip\nconst porta = msg.payload.porta\nconst usuario = msg.payload.usuario\nconst senha = msg.payload.senha\nconst nomeBD = msg.payload.nomeBD\n\nmsg.server = {\n \"host\": ip,\n \"port\": porta,\n \"username\": usuario,\n \"password\": senha,\n \"db\": nomeBD\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":280,"wires":[["88e81ef0f7ecf365"]]},{"id":"8cc0b55b7d0743c0","type":"change","z":"ea86445f05051d5d","name":"copy flow/global to msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.ip","pt":"msg","to":"IP_vGlobalBD","tot":"global"},{"t":"set","p":"payload.porta","pt":"msg","to":"Porta_vGlobalBD","tot":"global"},{"t":"set","p":"payload.usuario","pt":"msg","to":"Usuario_vGlobalBD","tot":"global"},{"t":"set","p":"payload.senha","pt":"msg","to":"Senha_vGlobalBD","tot":"global"},{"t":"set","p":"payload.nomeBD","pt":"msg","to":"NomeBD_vGlobalBD","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":220,"wires":[["effa65099dd2a525"]]}]

```

It's just that I'm trying to get good at flow language. I want to avoid functions as much as possible.

---

<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:** [26 January 2023 14:41 UTC](https://discourse.nodered.org/t/json-global-variables-in-the-change-node/74216/2 "2023-01-26T14:41:43Z")

</div>

You can use the JSONata expression type with the `$globalContext()` function to look something up.

Here's a Change node that creates `msg.server` with just the `host` property set - you should be able to add all the others to the expression.

```auto
[{"id":"eb96b8b79efaea17","type":"change","z":"c3d095d322ddcf1d","name":"","rules":[{"t":"set","p":"server","pt":"msg","to":"{ 'host': $globalContext('IP_vGlobalBD')}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":760,"wires":[["c8b1a934e6529a99"]]}]

```

The expression is:

```auto
{
   'host': $globalContext('IP_vGlobalBD')
}

```

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [26 January 2023 14:50 UTC](https://discourse.nodered.org/t/json-global-variables-in-the-change-node/74216/3 "2023-01-26T14:50:29Z")

</div>

I am very happy that the founder and creator of node-red responded to my topic.

Congratulations the tool you created is excellent, and I love using it. I'm a big admirer of your work.

Thanks for answering my question. I'm already putting it into practice.v

---

<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:** [9 February 2023 14:50 UTC](https://discourse.nodered.org/t/json-global-variables-in-the-change-node/74216/4 "2023-02-09T14:50:43Z")

</div>

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