# Read msg.payload\[variable\], identifier as variable string

**URL:** <https://discourse.nodered.org/t/read-msg-payload-variable-identifier-as-variable-string/45812>\
**Category:** General\
**Created:** [18 May 2021 06:04 UTC](https://discourse.nodered.org/t/read-msg-payload-variable-identifier-as-variable-string/45812 "2021-05-18T06:04:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doebe](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@Doebe](https://discourse.nodered.org/u/Doebe)\
**Post date:** [18 May 2021 06:04 UTC](https://discourse.nodered.org/t/read-msg-payload-variable-identifier-as-variable-string/45812/1 "2021-05-18T06:04:20Z")

</div>

Hi,

is his possible in a function to read the msg.payload  
with a variable identifier ?  
eg.

var tagname = node.name;

// Get name of node eg. LSH3-1.0 (level switch high)  
// in the msg.payload there is a value  
// LSH\_3\_1\_0 : 40  
// So var value could be  
// value = msg.payload.LSH\_3\_1\_0

var tag  
tag = tagname.replace(".", "_");  
tag = tag.replace("-", "_");  
var value = msg.payload[tag];  
var ON = (value & (1 \<\< 27)) === 0 ? 0 : 1;  
msg.payload = {};  
msg.payload = ON;  
msg.value = value;  
msg.tagname = tagname;  
return msg;

Whats is the correct syntax for this ?

Gr.

---

<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:** [18 May 2021 07:07 UTC](https://discourse.nodered.org/t/read-msg-payload-variable-identifier-as-variable-string/45812/2 "2021-05-18T07:07:33Z")

</div>

> [@Doebe](#):
>
> // Get name of node eg. LSH3-1.0 (level switch high)  
> // in the msg.payload there is a value  
> // LSH\_3\_1\_0 : 40  
> // So var value could be  
> // value = msg.payload.LSH\_3\_1\_0

As you say, the name of the function is `LSH3-1.0` then calling `.replace(".", "");` and `.replace("-", "")` will create `LSH310` which is NOT equal to `LSH_3_1_0` (so wouldnt work)

However, if you name the function `LSH_3_1_0` then this would work...

```javascript
var tagname = node.name;
var value = msg.payload[tagname];
var ON = (value & (1 << 27)) === 0 ? 0 : 1; 
msg.payload = ON;
msg.value = value;
msg.tagname = tagname;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Doebe](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@Doebe](https://discourse.nodered.org/u/Doebe)\
**Post date:** [18 May 2021 08:24 UTC](https://discourse.nodered.org/t/read-msg-payload-variable-identifier-as-variable-string/45812/3 "2021-05-18T08:24:44Z")

</div>

Ok thx for pointing that out, got it working now

---

<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:** [1 June 2021 08:25 UTC](https://discourse.nodered.org/t/read-msg-payload-variable-identifier-as-variable-string/45812/4 "2021-06-01T08:25:15Z")

</div>

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