# Best way to access dynamic msg payload?

**URL:** <https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170>\
**Category:** General\
**Created:** [15 April 2019 12:59 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170 "2019-04-15T12:59:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 12:59 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/1 "2019-04-15T12:59:09Z")

</div>

Good day,

I have an issue whereby I need to access a data inside a dynamically generated payload, the position I need to access is always the same, but the name of a certain level is generated as part of the return...

example:

msg.raw. **thischanges**.price

I thought I could access it via:

`var example = msg.raw[0].price` but this doesn't work.

What's the best way to achieve this using a function node?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 April 2019 13:05 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/2 "2019-04-15T13:05:08Z")

</div>

> [@swiftnesses](#):
>
> What's the best way to achieve this using a function node?

Is there always exactly one property under `msg.raw` ? And that is the property you care about? Or could there be other properties there, and if so, how do you know which is the one you want?

If there is exactly one, then you could use a Change node using a JSONata expression. Configure the node to set `msg.myValue` to the expression `$.raw.*.price`. Pass that result to your Function node where you can use `msg.myValue` however you want.

---

<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:** [15 April 2019 13:12 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/3 "2019-04-15T13:12:58Z")

</div>

Feed the message into a debug node set to show complete message and post it here so we can see exactly what you are dealing with.

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:15 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/4 "2019-04-15T13:15:53Z")

</div>

The payload looks like this... The level after RAW will vary (so I need some kind of wildcard to skip it)...

 ![01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b12741be6e63bcd505af0be6be67d4fe14564ff0.png)

---

<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:** [15 April 2019 13:20 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/5 "2019-04-15T13:20:12Z")

</div>

I note that it is msg.payload.RAW, not msg.raw.  
You can iterate over the properties of RAW using

```auto
for (var property in msg.payload.RAW) {
    if (object.hasOwnProperty(property)) {
        // do stuff with each property of RAW
    }
}

```

But @knolleary's suggestion is better I think. Though you will have to change it for the fact that it is in the payload and it is RAW not raw.

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:24 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/6 "2019-04-15T13:24:51Z")

</div>

Hi,

There could in fact be multiple values below RAW.THISCHANGES

example:

![59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/504c9781c2d0c5c529ee9f78a5dab8333c41bcf4.png)

I can extract the 'thischanges' value easily, so I wondered if it was perhaps possible to use this, something like:

var thischanges = msg.requested.content

var usd = msg.RAW.thischanges.USD.PRICE

I realise this doesn't work, but that's the kind of solution I was hoping for...

---

<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:** [15 April 2019 13:27 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/7 "2019-04-15T13:27:06Z")

</div>

Have you tried the solutions suggested?

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:27 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/8 "2019-04-15T13:27:24Z")

</div>

You can see here I'm already extracting the request, i.e. BTC

![44](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/80f3ee8ae8cab59eb7dde293182d1079c9ee74e2.png)

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:28 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/9 "2019-04-15T13:28:01Z")

</div>

Hi Colin,

Apologies, I'm not really sure I understand how to implement / test them...

---

<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:** [15 April 2019 13:29 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/10 "2019-04-15T13:29:16Z")

</div>

If you already know the value of 'thischanges' then you can do  
`msg.payload.RAW[thischanges]`  
where `thischanges`is a variable.  
I note, however that the the content attribute you show has a space before BTC whereas the attribute name doesn't

---

<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:** [15 April 2019 13:31 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/11 "2019-04-15T13:31:02Z")

</div>

Alternatively, what don't you understand about @knolleary's suggestion?

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:36 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/12 "2019-04-15T13:36:02Z")

</div>

> [@knolleary](#):
>
> $.raw.\*.price

Hi Colin,

I trim the space normally, it's also saved to another message (trimmed).

I built this, as suggested and it works, but means having more data floating around in payloads...

 ![40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/20e0529587c9a9dda7a05fae057f05cdda2fef2f.png)

If only I could use these expressions in the function to extract the numbers from the payload, it would be perfect!

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:41 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/13 "2019-04-15T13:41:10Z")

</div>

I also need to extract about 10-15 values from each item below 'thischanges', so not sure I can have / maintain so many change nodes...

I assume one option is to remove 'thischanges' from the payload before using the function node (so I can use normal msg references), can this be done?

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [15 April 2019 13:49 UTC](https://discourse.nodered.org/t/best-way-to-access-dynamic-msg-payload/10170/14 "2019-04-15T13:49:40Z")

</div>

I placed this before my function node, now I can remove the changing object and use normal msg references, thanks for the tips guys!

 ![50](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/75a87f00a9ec63a90ee664b7a63bbe515cf18950.png)
