# Variable value as part of name of the another variable

**URL:** <https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793>\
**Category:** General\
**Created:** [7 April 2021 05:31 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793 "2021-04-07T05:31:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![petrasl](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@petrasl](https://discourse.nodered.org/u/petrasl)\
**Post date:** [7 April 2021 05:31 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793/1 "2021-04-07T05:31:45Z")

</div>

Hi, plese help a newbie which can't even formulate its questions properly 🙂 I have two of them:

1. Lets say i have a switch, which after action sends what was done like a confirmation that task was done: `http://...../switch?id=sw1&action=off` or `...&action=on` - this is my **httpIn** node.  
I want to store this value in to **flow.sw1.action** variable. Is it possible to do this with "change" node?

I tried **set** `flow.{{msg.payload.id}}.action` to `0`, but after i looked into **context data** tab i found some mess with all this curly brackets inside ☹

1. Before my switch performs any action it does similar call to the same **httpIn** node to get current state - `http://...../switch?id=sw1&action=get`  
I want to format response based on another variable **flow.sw1.state** and return it to my switch.

I tried to use **template** node and form responce like this `{{flow.{{msg.payload.id}}.state}}`, but again, result not as i expected. I get `.req}}.state}}` in the response body.

How can i use variable value as part of name of the another variable?

Regards  
PetrasL

---

<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:** [7 April 2021 06:35 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793/2 "2021-04-07T06:35:24Z")

</div>

Here is a example of writing to context and reading from context,  
Hope it helps.

```auto
[{"id":"b26327a4.75fa9","type":"inject","z":"5a245aa1.510164","name":"http input on","props":[{"p":"id","v":"sw1","vt":"str"},{"p":"action","v":"on","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":4760,"wires":[["bc0ba1e5.fe905"]]},{"id":"bc0ba1e5.fe905","type":"function","z":"5a245aa1.510164","name":"","func":"flow.set(\"switches.\"+msg.id+\".action\", msg.action)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":4780,"wires":[["f749d945.96ee8"]]},{"id":"f6078b34.02cbe","type":"inject","z":"5a245aa1.510164","name":"http input off","props":[{"p":"id","v":"sw1","vt":"str"},{"p":"action","v":"off","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":4800,"wires":[["bc0ba1e5.fe905"]]},{"id":"f749d945.96ee8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$flowContext(\"switches\")","targetType":"jsonata","statusVal":"","statusType":"auto","x":530,"y":4780,"wires":[]},{"id":"ac0f1bce.49ff","type":"inject","z":"5a245aa1.510164","name":"http input","props":[{"p":"payload.id","v":"sw1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":210,"y":4860,"wires":[["a6a0bc6a.f634a"]]},{"id":"a6a0bc6a.f634a","type":"function","z":"5a245aa1.510164","name":"","func":"msg.payload = flow.get(\"switches.\"+msg.payload.id+\".action\") || \"error\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":4860,"wires":[["62c5ab65.e56f9c"]]},{"id":"62c5ab65.e56f9c","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":4860,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![droberts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/droberts/32/40295_2.png) [@droberts](https://discourse.nodered.org/u/droberts)\
**Post date:** [7 April 2021 06:40 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793/3 "2021-04-07T06:40:05Z")

</div>

I was busy putting together something very similar to E1 - If you want to store dynamic variable names in the context, that is pretty much the only way to do it, by appending strings together in a function node where you set the values.

---

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [7 April 2021 07:04 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793/4 "2021-04-07T07:04:55Z")

</div>

Hi, Happy to help.I have gone down the function node path assuming you need to extract the parameters from the httpIn call. I have also assum Are you trying to parse the httpIn request to get the query parameters and save them to be used later. There are many ways to solve this but I have tried to stay close to your path. It may be possible in a change node but will leave it to JSONata experts (I am not)

Add a function node after the httpIn and parsre the queryParams. They are made avaialbe in

```auto
msg.req.query

```

The javascript secret to use a varaible name as part of an object is to enclose in brackets

```auto
let queryParams = msg.req.query

let switch_obj = {}
if (queryParams){
     id = queryParams.id ? queryParams.id : null // what do you want to do here. I assume id should be mandatory ?
     action = queryParams.action ? queryParams.action : null
     if (id && action){ 
      switch_obj = {
          id : id,
         action: action
      }
   }   
}

if (switch_obj){
     flow.set([switch_obj.id],switch_obj)
}

```

To get the switch object you could now do a

```auto
let switch_obj = flow.get("sw1")
let action = switch_obj.action

```

Does this make sense ?

---

<div class="post-metadata">

**Author:** ![petrasl](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@petrasl](https://discourse.nodered.org/u/petrasl)\
**Post date:** [7 April 2021 15:47 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793/5 "2021-04-07T15:47:22Z")

</div>

Thanks, guys! Both solutions works for me. 🙂

---

<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 April 2021 15:47 UTC](https://discourse.nodered.org/t/variable-value-as-part-of-name-of-the-another-variable/43793/6 "2021-04-21T15:47:49Z")

</div>

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