# Access a payload property by using a string variable

**URL:** https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981
**Category:** General
**Created:** [13 September 2021 20:59 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981 "2021-09-13T20:59:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Daniele-Tampieri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daniele-tampieri/32/40688_2.png) [@Daniele-Tampieri](https://discourse.nodered.org/u/Daniele-Tampieri)
#### Post date: [13 September 2021 20:59 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/1 "2021-09-13T20:59:25Z")

</div>

**Premise**  
My question is related to [this Q&A](https://discourse.nodered.org/t/combine-strings-from-multiple-inputs/45637) as my problem is similar. I have a `msg.payload` which has several properties (as a matter of facts arrays, but this is not meaningful for the question) whose name is `section_x` where `x` is an integer which ranges fro 1 to at most 5 (I do not know a priori its value as it is determined by the action of several flows on the payload). I have to access one of these properties indexed by an integer `i` producer by another flow. I tried the following code following the cited Q&A

```auto
var v = [];
v = msg.payload.`section_${i}`; 
return msg;

```

but I did not succeed as the code does not compile. Now the **questions** :

1. Can I access a payload property by using a similar expression? If yes, how?
2. Where can I find examples of uses of the code as the one shown by Steve-Mcl in the Q&A cited above?

---

<div class="post-metadata">

### Author: ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)
#### Post date: [13 September 2021 21:04 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/2 "2021-09-13T21:04:32Z")

</div>

> [@Daniele-Tampieri](#):
>
> ```auto
> v = msg.payload.`section_${i}`; 
> return msg;
> 
> ```

I don't know if you code is the correct way (without a good think about it 😛 )... but I do know that you have to return the contents of `v` back to `msg.payload` before the `return msg`

`msg.payload = v;`

---

<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: [13 September 2021 21:40 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/3 "2021-09-13T21:40:30Z")

</div>

Use `RED.util.getObjectProperty`

See similar thread here : [Alternatives to eval() - #2 by Steve-Mcl](https://discourse.nodered.org/t/alternatives-to-eval/50550/2)

in your case...

```auto
var i = 1; // <<get from somewhere else. Hard coded to 1 for demo purposes
var varName = `section_${i}`
var v = RED.util.getObjectProperty(msg.payload, varName); 

```

---

<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: [13 September 2021 22:41 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/4 "2021-09-13T22:41:18Z")

</div>

> [@Daniele-Tampieri](#):
>
> ```auto
> var v = [];
> v = msg.payload.`section_${i}`; 
> return msg;
> 
> ```

[] notation should work.

```auto
let i = "1";
msg.payload = msg.payload[`section_${i}`]; 
return msg;

```

---

<div class="post-metadata">

### Author: ![Daniele-Tampieri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daniele-tampieri/32/40688_2.png) [@Daniele-Tampieri](https://discourse.nodered.org/u/Daniele-Tampieri)
#### Post date: [19 September 2021 21:00 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/5 "2021-09-19T21:00:46Z")

</div>

Wonderful, I tried it and it worked perfectly. Thank you, Steve.

---

<div class="post-metadata">

### Author: ![Daniele-Tampieri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daniele-tampieri/32/40688_2.png) [@Daniele-Tampieri](https://discourse.nodered.org/u/Daniele-Tampieri)
#### Post date: [19 September 2021 21:14 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/6 "2021-09-19T21:14:28Z")

</div>

I also tried your solutions and it works perfectly too: I decided to chose Steve's solution only since it suits better in my current project. However, I'll use yours in other occasions. Thank you, E1cid

---

<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 October 2021 21:15 UTC](https://discourse.nodered.org/t/access-a-payload-property-by-using-a-string-variable/50981/7 "2021-10-03T21:15:13Z")

</div>

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