# How to parse message parts to variables

**URL:** <https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192>\
**Category:** General\
**Created:** [25 January 2023 21:09 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192 "2023-01-25T21:09:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 21:09 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/1 "2023-01-25T21:09:16Z")

</div>

Sorry folks, I'm stuck again.

Story:  
I have a message (with a LOT of extra stuff added on to save me using `flow` context.) and want to use one part to point to another part of the message.

eg:

 ![Screenshot from 2023-01-26 08-00-07](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d45e513c20075400775e907cd302f9e57258176a.png)

So:  
`msg.payload.who` gives me `S21`. I then want to _translate_ that to what that part of the message is. _In this case msg.S21_

But I can't remember the trick needed to do that.

Someone - please.  
I know it is easy, but I can't remember.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 January 2023 21:14 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/2 "2023-01-25T21:14:20Z")

</div>

```auto
node.warn(`LED ${ msg.payload[msg.payload.who] }`)

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 21:14 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/3 "2023-01-25T21:14:44Z")

</div>

(Ok, I won't repost the screen shot.)

I saw the mistake there.

But it doesn't solve the problem.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 21:17 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/4 "2023-01-25T21:17:23Z")

</div>

I really must have pissed _the gods_ off.

 ![Screenshot from 2023-01-26 08-16-03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/286c0c4fe16362c2cdaec662487ab9906d2c1a8d.png)

That is right - yes?  
(But not getting the right result.)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 January 2023 21:18 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/5 "2023-01-25T21:18:13Z")

</div>

Can you share the input object please - makes it a lot easier to come up with correct code.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 January 2023 21:19 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/6 "2023-01-25T21:19:19Z")

</div>

And to prove the point, I didn't see in the original message that it needs to be `msg[msg.payload.who]`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 21:19 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/7 "2023-01-25T21:19:32Z")

</div>

Sorry...

Here's the code. (Cut down)

```auto
[{"id":"cbce6faf1a990d00","type":"inject","z":"a1911aa3.c45be8","name":"Request 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"request","payloadType":"str","x":1680,"y":3140,"wires":[["aa56b0127f924373"]]},{"id":"aa56b0127f924373","type":"change","z":"a1911aa3.c45be8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"who\":\"S21\",\"power\":\"REQUEST2\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1795,"y":3140,"wires":[["5cb9d1e4615b4047"]],"l":false},{"id":"2d11cf195d61c08d","type":"change","z":"a1911aa3.c45be8","name":"POWER Colours","rules":[{"t":"set","p":"REQUEST","pt":"msg","to":"REQUEST","tot":"flow"},{"t":"set","p":"REQUEST2","pt":"msg","to":"REQUEST2","tot":"flow"},{"t":"set","p":"DETECTED","pt":"msg","to":"DETECTED","tot":"flow"},{"t":"set","p":"FULL","pt":"msg","to":"FULL","tot":"flow"},{"t":"set","p":"REMOVED","pt":"msg","to":"REMOVED","tot":"flow"},{"t":"set","p":"S21","pt":"msg","to":"2","tot":"num"},{"t":"set","p":"S5","pt":"msg","to":"3","tot":"num"},{"t":"set","p":"other","pt":"msg","to":"4","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1860,"y":3450,"wires":[["f1e91ce466ceef93","796148bfb7573c9f","430a11c880744cea"]]},{"id":"5cb9d1e4615b4047","type":"junction","z":"a1911aa3.c45be8","x":1720,"y":3450,"wires":[["2d11cf195d61c08d"]]},{"id":"430a11c880744cea","type":"function","z":"a1911aa3.c45be8","name":"function 14","func":"node.warn(\"Who \" + msg.payload.who);\n\nnode.warn(`LED ${msg.payload[msg.payload.who]}`)\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2000,"y":3570,"wires":[[]]}]

```

But I think it is something to do with the `msg.S21` is NOT `msg.payload.S21`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 January 2023 21:19 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/8 "2023-01-25T21:19:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> But I think it is something to do with the `msg.S21` is NOT `msg.payload.S21`

Yup, saw it in the 2nd image.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 21:22 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/9 "2023-01-25T21:22:39Z")

</div>

When I trim down what you offered from:  
`node.warn(`LED ${msg.payload[msg.payload.who]}`)`

to

`node.warn(`LED ${msg.payload[msg.who]}`)`

It doesn't work either.

Unless I have missed another elephant.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 January 2023 21:23 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/10 "2023-01-25T21:23:47Z")

</div>

> [@Trying\_to\_learn](#):
>
> node.warn(`LED ${msg.payload[msg.who]}`)

Wrong way round!

```auto
node.warn(`LED ${msg[msg.payload.who]}`)

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 21:25 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/11 "2023-01-25T21:25:50Z")

</div>

I'm so sorry.

Thanks.

That's it.

It would be nicer - for both you and me - if I could get my head around the structure.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 January 2023 00:11 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/12 "2023-01-26T00:11:45Z")

</div>

I understand the square brackets to reference the value of msg.payload,who

But can you explain the ${ } and backticks?

```auto
node.warn(`LED ${msg[msg.payload.who]}`)

```

and

```auto
node.warn("LED " + msg[msg.payload.who])

```

seem to be equivalents?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 January 2023 00:34 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/13 "2023-01-26T00:34:29Z")

</div>

> [@jbudd](#):
>
> ${ } and backticks

Sure, the backticks define a JavaScript template literal string. Such strings are allowed to contain JavaScript code inside a `${...}`.

This is actually more efficient than `"string " + something`. And often neater code as well. Part of ES6. Template literals allow multi-line strings too.

> [@jbudd](#):
>
> seem to be equivalents?

They are! Other than the fact that the JavaScript JIT can pre-compile much of the string making the whole thing more efficient.

For some extra fun, here is a simple JS function that lets you put HTML into a string. Used in VScode, you get syntax highlighting inside the string. 🙂 Some libraries have similar extended string template functions. This is called a "tagged template".

```auto
function html(strings, ...keys) {
    return strings.map( (s, i) => {
        return s + (keys[i] || '')
    }).join('')
}

// https://www.makeuseof.com/responsive-navigation-bar-using-html-and-css/
const myHtml = html`
    <style>
        :host {
            width: 100%;
            background-color: black;
            color: white;
            padding: 0;
            margin: 0 0 0.8rem 0;
            /* border: 2px solid silver; */
        }
    </style>
`

```

This is a snippet from one of my example HTML Components.

[Template literals (Template strings) - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

Though I now see an even simpler version of the above from the MDN example:

```auto
const html = (strings, ...values) => String.raw({ raw: strings }, ...values)

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 January 2023 00:49 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/14 "2023-01-26T00:49:34Z")

</div>

Thanks! 😵‍💫

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 January 2023 00:51 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/15 "2023-01-26T00:51:30Z")

</div>

Haha. Well, the tagged template may be a bit esoteric. But template strings are really useful and greatly simplify a lot of code where you are combining strings and variables which is a very common requirement.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 January 2023 01:01 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/16 "2023-01-26T01:01:58Z")

</div>

(I hear you @jbudd )

I am still not getting it for some reason.

Going back to this:

`node.warn(`LED ${msg.payload[msg.who]}`)`

(Ok, why am I not getting the colours?)

That will put a `node.warn` message out.

There is a pointer which is `msg.payload.who`.  
This points to the part of the `msg` I want to get.

So I use the _pointer_ of `msg.payload.who` to construct the new `msg`  
Ok, `pointer` is the wrong word I fear..... but for now.

ARGH! This is doing my nut it.

Maybe made worse because I don't know the correct terms to use.

Ok:  
The _contents_ of `msg.payload.who` (S21).  
So I need to get the contents of `msg.S21` (2)

I am really missing something.  
The `payload` in there.... I thought the _pointer_ (index?) was `msg.payload.who` I am not getting how the `msg.payload[msg.who]` bit works. Because it is `msg.payload.who` according to the picture I posted - and I am sure that is the message structure I have since adopted.

If it isn't too difficult......

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 January 2023 01:07 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/17 "2023-01-26T01:07:15Z")

</div>

> [@Trying\_to\_learn](#):
>
> There is a pointer which is `msg.payload.who`.

Good enough term.

> [@Trying\_to\_learn](#):
>
> So I use the _pointer_ of `msg.payload.who` to construct the new `msg`

Really, you are using the value of the `who` property of `msg.payload` as the reference to a property of `msg`.

> [@Trying\_to\_learn](#):
>
> I am not getting how the `msg.payload[msg.who]` bit works.

That's because IT DOESN'T! You need `msg[msg.payload.who]` as I've said!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 January 2023 01:10 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/18 "2023-01-26T01:10:03Z")

</div>

Sorry....

I am really not keeping up.  
(My fault.)

I was a bit distracted with getting that stuff on the subflow working and that was a bit painful.  
But it now works.

So I thought I would catch up on the help you gave me and (obviously) stumbled at the get go.

Ok, I'll go back and look at it again.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 January 2023 01:11 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/19 "2023-01-26T01:11:11Z")

</div>

Bedtime in the UK. Catch you tomorrow.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 January 2023 09:02 UTC](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192/20 "2023-01-26T09:02:11Z")

</div>

Apologies, I had msg.who and msg.payload.who confused above. I've edited it.

Restating this to solidify my own understanding...

These are all different ways to obtain the value of msg.S21  
(in the above example msg.payload.who = "S21" and msg.S21 = 2)

node.warn("LED " + msg.S21)  
node.warn("LED " + msg["S21"])  
node.warn("LED " + msg[msg.payload.who])

And I can get the same thing using a template literal (i.e. enclosed in backticks)  
Except that inside the backticks, any javascript must be wrapped in ${ and }

```auto
node.warn(`LED ${msg[msg.payload.who]}`)

```

[Next page](https://discourse.nodered.org/t/how-to-parse-message-parts-to-variables/74192.md?page=2)
