Best way to access dynamic msg payload?

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?

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.

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.

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

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

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.

Hi,

There could in fact be multiple values below RAW.THISCHANGES

example:

59

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...

Have you tried the solutions suggested?

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

44

Hi Colin,

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

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

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

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...

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

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?

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