# How to set a global var with dynamic index on change node

**URL:** https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627
**Category:** General
**Created:** [18 August 2022 22:55 UTC](https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627 "2022-08-18T22:55:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![VMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vmarques/32/31221_2.png) [@VMarques](https://discourse.nodered.org/u/VMarques)
#### Post date: [18 August 2022 22:55 UTC](https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627/1 "2022-08-18T22:55:35Z")

</div>

Hi There guys,

I am trying to,

- read a file containing a json string (all fine here),
- parse the keys and declare global vars with the name from the json,

The flow looks like this,

 ![Screenshot 2022-08-18 at 23.50.52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fdea187e6cac0dd9aabeca73e5da5a752803eeb2.png)

Split node loads the var name in msg.index

 ![Screenshot 2022-08-18 at 23.50.33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e13dcc9af2cc0e5053ad7b0e313b48efc01fb62e.png)

Here is my problem, I can't get the value of msg.index, tried moustache, @$, eval(), and some others, the variable is always declared with the literal content of the edit,

 ![Screenshot 2022-08-18 at 23.50.09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c87a8914c469628916066988732ac9b755fbe03.png)

In short, how to read the a var content in a edit box - on the change node,

Thanks for any help,

---

<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 August 2022 23:24 UTC](https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627/2 "2022-08-18T23:24:15Z")

</div>

you could use [] notation, but it only works on a named property of the context in the change node (node-red \> 1.3).  
e.g  
`global.`foo[msg.index] would work  
but  
`global.`[msg.index] does not.

you can do  
`global.set(msg.index, msd.payload);`  
in a function node, if you can not work with an extra depth to your global vars.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [18 August 2022 23:32 UTC](https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627/3 "2022-08-18T23:32:08Z")

</div>

In short, based on what @E1cid mentioned above. You can use a code like below inside a function node. You pass a json object inside `msg.payload` ang get as result a set of global variables created.

```auto
let pay = msg.payload;

for (let prop in pay) {
    global.set(prop, pay[prop])
}
return msg;

```

For instance, this following json:

```auto

{
    "aaa": 10,
    "bbb": 20
}

```

will result in the following global variables:

![g1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96e606b5f56383bd0f3cc191f4607c60dbbe81f9.png)

---

<div class="post-metadata">

### Author: ![VMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vmarques/32/31221_2.png) [@VMarques](https://discourse.nodered.org/u/VMarques)
#### Post date: [20 August 2022 00:10 UTC](https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627/4 "2022-08-20T00:10:08Z")

</div>

Super, it works like a charm. Thanks you guys !

sharing a thought... it would be useful on the change node to allow dynamic var naming. The documentation discourages you to use coded interactions, unavoidable in this particular case.

---

<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 September 2022 00:10 UTC](https://discourse.nodered.org/t/how-to-set-a-global-var-with-dynamic-index-on-change-node/66627/5 "2022-09-03T00:10:23Z")

</div>

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