# Dynamically select msg.payload key element

**URL:** <https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483>\
**Category:** General\
**Tags:** function-node\
**Created:** [28 June 2022 21:32 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483 "2022-06-28T21:32:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hameljc](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hameljc](https://discourse.nodered.org/u/hameljc)\
**Post date:** [28 June 2022 21:32 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/1 "2022-06-28T21:32:53Z")

</div>

I have looked around but found nothing that answer my situation.

I want to setup a global variable. The value of the variable will come from the msg.payload BUT, the element name (key) will be dynamically build like so:

global.set("ConnectServerUrl",msg.payload.ConnectServerUrl **\<-DEV,-TEST,-PROD\>** )

The **-DEV** or **-TEST** or **-PROD** is from another global variable name **global.environment**.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 June 2022 07:31 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/2 "2022-06-29T07:31:44Z")

</div>

Welcoome to the forum @hameljc

Try something like

```auto
global.set("ConnectServerUrl",`${msg.payload.ConnectServerUrl} ${global.get("environment")}`)

```

That is assuming that the global variable is "environment" not global.environment. It would probably not be a good idea to have a global variable called global, that would surely be confusing.

The backtick syntax is called template literals, see [JavaScript Template Literals](https://www.w3schools.com/js/js_string_templates.asp)

---

<div class="post-metadata">

**Author:** ![hameljc](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hameljc](https://discourse.nodered.org/u/hameljc)\
**Post date:** [29 June 2022 15:21 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/3 "2022-06-29T15:21:19Z")

</div>

> [@Colin](#):
>
> `global.set("ConnectServerUrl",`${msg.payload.ConnectServerUrl} ${global.get("environment")}`)`

Thanks for your reply but the concatenation doesn't need to be with the value of **msg.payload.ConnectServerUrl** but rather to define/name the element/key ConnectServerUrl like so:  
**msg.payload.ConnectServerUrl-DEV**  
**msg.payload.ConnectServerUrl-TEST**  
**msg.payload.ConnectServerUrl-PROD**

The **-DEV** or **-TEST** or **-PROD** will come from another global variable name **environment**.

I am looking for a way to dynamically set the name of the key in the **msg.payload**

---

<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:** [29 June 2022 16:09 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/4 "2022-06-29T16:09:49Z")

</div>

If i understand correctly, try

```auto
const key = "ConnectServerUrl-" + (global.get("environment") || "default");
global.set("ConnectServerUrl", msg.payload[key]);

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 June 2022 16:20 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/5 "2022-06-29T16:20:06Z")

</div>

If that isn't it then give us some examples of actual values in the globals and msg.payload and tell us what you want to happen.

---

<div class="post-metadata">

**Author:** ![hameljc](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hameljc](https://discourse.nodered.org/u/hameljc)\
**Post date:** [29 June 2022 18:16 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/6 "2022-06-29T18:16:39Z")

</div>

Thank you [E1cid](https://discourse.nodered.org/u/E1cid), exactly what I needed 😃

---

<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:** [28 August 2022 18:17 UTC](https://discourse.nodered.org/t/dynamically-select-msg-payload-key-element/64483/7 "2022-08-28T18:17:21Z")

</div>

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