# How to write code to point to/get message subparts?

**URL:** <https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830>\
**Category:** General\
**Created:** [27 April 2022 01:27 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830 "2022-04-27T01:27:18Z")\
**Posts on this page:** 7\
**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:** [27 April 2022 01:27 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/1 "2022-04-27T01:27:18Z")

</div>

I am writing this as I am on another quest to try and get my head around how to do something.

Taking it to basics - the flow:

```auto
[{"id":"75a8babad6400d8f","type":"inject","z":"7e987ddf260bdf0d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":4180,"y":2310,"wires":[["47405ea11fe09c0c"]]},{"id":"47405ea11fe09c0c","type":"function","z":"7e987ddf260bdf0d","name":"Example 2","func":"msg = { \"WIFI_DEVICE\": \"TelePi\", \"IP_Address\": \"192.168.0.93\", \"Sent\": \"2022-04-27 07:24:54\" };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4350,"y":2310,"wires":[["6b24df8a1f645f03","50fe4009ba4846bf"]]},{"id":"6b24df8a1f645f03","type":"function","z":"7e987ddf260bdf0d","name":"","func":"let x = \"WIFI_DEVICE\";\nlet y = \"msg.\"+x;\nnode.warn(y);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4520,"y":2310,"wires":[[]]}]

```

The message being sent in is this (_example_)  
`{"WIFI_DEVICE":"TelePi","IP_Address":"192.168.0.93","Sent":"2022-04-27 07:24:54","_msgid":"bf56e1984a7c7ab2"}`

In the second `function` node I want to define which _sub part_ of the message I want to _get/see_.

Alas rather than getting the value, I get the path.

This may be easier to explain:

```auto
let x = "WIFI_DEVICE";
let y = "msg."+x;
node.warn(y);
return msg;

```

`x` is the _path_ I create the path name (`let y = "msg."+x`) but then I get that echoed to me.  
I want to see the value of where that is _pointing_. (I think is the term)

So I should get `TelePi`.

Efforts:

```auto
let x = "WIFI_DEVICE";
let y = "msg."+x;
node.warn(y); // a
node.warn(msg.WIFI_DEVICE); // b
node.warn(msg.x); // c
return msg;

```

`a` gets what I've shown.  
`b` works.  
`c` doesn't.  
Trying to .... _concatenate_ the `msg.` and `x` isn't working if I put a `+` sign.

Ok, maybe I am wrong with one of those... I'm working on it now, in real time.  
It's impossible to keep the thread up to date with what is happening now.

I'm asking if someone could confirm how to do this before I go any more batty than I already am. 😉

---

<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:** [27 April 2022 03:09 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/2 "2022-04-27T03:09:32Z")

</div>

Use square bracket notation...

```auto
const part="payload"
let p = msg[part]
msg[part] = 123

```

---

<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:** [27 April 2022 03:11 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/3 "2022-04-27T03:11:22Z")

</div>

Of course! Silly me!

And in the mean time I have found **another** way of doing it. Messy.... But....

But now I know this, I'll keep things simple and do it that way.

Argh! I should have known that.

Thanks mate.

---

<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:** [27 April 2022 22:53 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/4 "2022-04-27T22:53:03Z")

</div>

Hey Steve.....

It did work, but it also doesn't work.

I think I oversimplified the example as the initial tests after you showed me did work.

But now I am onto the REAL application it is back to not wanting to play the game.

This is the payload I am using now - which is a bit more complex (I suspect) than the original one.

`{"topic":"STATUS/WIFIDEVICEID","payload":{"WIFI_DEVICE":"TelePi","IP_Address":"192.168.0.93","Sent":"2022-04-28 08:18:58"},"qos":0,"retain":false,"_msgid":"31994a5118c3e254"}`

(This has `payload` and nested stuff in it.)

This is a screen shot of what I see:

 ![Screenshot from 2022-04-28 08-46-28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/3462d15cb017215bd3ebd85e763917d5a2deed4a.png)

To maybe help with that this is an exert from the code:

```auto

let part = env.get("msg_part_to_scan");
if (part.length < 1)
    throw "You must define which part of the message to scan";
//------------------

if (debug == 1)
    node.warn("Part is set to " + part); // You see this in the green box above.

// Set `newval` from incoming message.
newval = msg[part]; // Extract the desired part of the message.

if (debug == 1)
{
    node.warn("100 Mode is set to " + mode);
    node.warn("101 control_topic is set to " + CTLTopic);
    node.warn("102 Incoming message for checking is " + newval);
    node.warn("103 topic_mode is set to " + topic_mode);
    node.warn("104 Topic is " + topic);
}

```

Yet `102` isn't happy with what `newval` is.  
But I'm guessing it is failing where/when it gets it's assignment.  
`newval = msg[part]`

Seems I am still missing an elephant in the room somewhere.

# Oh

(For anyone)

Here are the two _example_ flows.  
Top works, bottom one doesn't.

```auto
[{"id":"e4726afe26a31f7e","type":"inject","z":"7e987ddf260bdf0d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":4260,"y":3230,"wires":[["295950a9264e5196"]]},{"id":"295950a9264e5196","type":"function","z":"7e987ddf260bdf0d","name":"Example 2","func":"msg = { \"WIFI_DEVICE\": \"TelePi\", \"IP_Address\": \"192.168.0.93\", \"Sent\": \"2022-04-27 07:24:54\" };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4430,"y":3230,"wires":[["8d19384085c188b1"]]},{"id":"8d19384085c188b1","type":"function","z":"7e987ddf260bdf0d","name":"","func":"let x = \"WIFI_DEVICE\";\nlet y = msg[x];\nnode.warn(y);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4600,"y":3230,"wires":[[]]},{"id":"0679f0af6fcb63c2","type":"inject","z":"7e987ddf260bdf0d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":4260,"y":3290,"wires":[["a43b64bf445f3611"]]},{"id":"a43b64bf445f3611","type":"function","z":"7e987ddf260bdf0d","name":"","func":"msg = {\"topic\":\"STATUS/WIFIDEVICEID\",\"payload\":{\"WIFI_DEVICE\":\"TelePi\",\"IP_Address\":\"192.168.0.93\",\"Sent\":\"2022-04-28 08:18:58\"},\"qos\":0,\"retain\":false,\"_msgid\":\"a90103fd34a05c17\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4420,"y":3290,"wires":[["5cdb79b33aab4766"]]},{"id":"5cdb79b33aab4766","type":"function","z":"7e987ddf260bdf0d","name":"","func":"let x = \"payload.WIFI_DEVICE\";\nlet y = msg[x];\nnode.warn(y);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4600,"y":3290,"wires":[[]]}]

```

---

<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:** [28 April 2022 07:12 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/5 "2022-04-28T07:12:38Z")

</div>

> [@Trying\_to\_learn](#):
>
> ```auto
> // Set `newval` from incoming message.
> newval = msg[part]; // Extract the desired part of the message.
> 
> ```

You are trying to access sub element of `payload` named `WIFI_DEVICE`...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b07c143bb62ff810a7b053a7ebe0458ca2afcd5.png)

This is NOT how [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#bracket_notation) works.

To use bracket notation with multiple levels, you would need to split the string & loop the parts to reach the 2nd level element

e.g. `msg[first_level][second_level]` which is the same as `msg["payload"]["WIFI_DEVICE"]` or `msg.payload.WIFI_DEVICE`

Fortunately, there is a helper utility function built into node-red...

![chrome_9Eg7I88izq](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc038c98395085ece4b785fda32e60f1a6a0ce34.gif)

[`RED.util.getMessageProperty`](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.getMessageProperty)

---

<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:** [28 April 2022 21:02 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/6 "2022-04-28T21:02:40Z")

</div>

@Steve-Mcl

Thanks.

Sorry for sort of misdirecting you with the previous _example_ which was kind of correct but also flawed because I didn't realise things were that complicated.

I rolled out the first solution and wondered why as much as some were working: others weren't.

---

<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:** [12 May 2022 21:03 UTC](https://discourse.nodered.org/t/how-to-write-code-to-point-to-get-message-subparts/61830/7 "2022-05-12T21:03:26Z")

</div>

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