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

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,

Split node loads the var name in msg.index

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,

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

Thanks for any help,

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[msg.index] would work
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.

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.

let pay = msg.payload;

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

For instance, this following json:

    "aaa": 10,
    "bbb": 20

will result in the following global variables:


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.

1 Like

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