Retrieving variables from payload object

I have constructed a flow which ends up turning a json string into an object with variables as follows:

payload: array[1]
0: object
DeviceId: "a name and number"
Site: "a city name"
UTC: "2019-07-10 07:54:49"
State: "start"

I am trying to use these variables (DeviceId, Site, UTC and State) and merge them into a string like "some text+DeviceId+some more text+Site" and so on. I have tried several ways of referencing the variables, but to no avail. Which is the correct way, if it is not one of the following?
msg.payload.0.DeviceId (the error here is that the function node I am using does not like the 0)
msg.payload.DeviceId (no output)
msg.payload[0] (no output)
msg.payload[0[DeviceId]] (no output)
I am one semicolon from pulling my hair out.:grimacing:

By default debug nodes only outputs payload objects

msg.payload[0].DeviceId should provide an output - if you configure the debug node to display the complete msg

There's a great page in the docs that's worth a read.
https://nodered.org/docs/user-guide/messages

This page explains how to use the tooltips in the debug node to ensure you get the right path to any bit of data.

Thanks for your reply.
I almost always use the "complete msg object" way of using debug nodes, so that is not the explanation.
I tried msg.payload[0].DeviceId, but still nothing;

10.7.2019 13.20.19 node: funktionsdebug efter
msg : Object
object
_msgid: "1234XYZ"
topic: ""
payload: array[1]
0: object
DeviceId: "device123"
Site: "cityname"
UTC: "2019-07-10 07:54:49"
State: "start"
...

Excellent, thanks. I had forgotten about that. The strange thing is, though, that even when I copy the path to DeviceId, which is allegedly "payload[0].DeviceId", I get nothing. Or actually, I get the error that this is not defined. I then put "msg." in front of it, but then there is just no trace of this in the debug node.

Anyways, is there an easier way of getting around this? I am currently using the json node to convert json data into an object, but what I really want is to get the information converted to variables with names and values. If this is in an array and/or an object is not important, though it seems to be my main problem right now.

Can you share what your actual Function code is?

Your original cut/paste from the Debug sidebar shows you have already converted the JSON string to an Object.

So msg.payload[0].DeviceId is correct way to access the property. If that isn't working, then there may be something else going on here.

Oh, just shoot me already.
In order to find out why this was not working, I was defining the variables separately instead of just putting them inside the string I need to send. What I thought was that the debug node would display the variables being defined in the node, but of course it isn't - at least unless they are part of the msg. So if I use

msg.DeviceId = msg.payload[0].DeviceId

it works - AND the variable is shown in the debug pane.
Thanks for trying to help me out, though I was just being a moron all along. :upside_down_face:

The debug pane doesn't show variables, it shows the contents of the message passed to it.

Hi all!
Just working on the same issue with no luck.

I have this error.
image

...when i want to access this element.
image

I've written a function to "get" that value only with no luck.
image

Any help out there?

Thanks a lot!

You have a space in the variable name.

Use the copy path button in the debug window to avoid the school boy errors. :wink:

Actually the space might be a graphical glitch. Regardless, the path is wrong - use the copy path button by hovering the item of interest in the debug window to avoid mistakes.

If you don't know what I mean, read the docs - "working with messages"

Uh thanks for the reply, there is no space on the variable name it has a underscore simbol but is not visible.

Hi @Marty1982

the screenshot shows the data you want to access is msg.data.new_state.timeSinceChangedMs. There is no payload[0] needed.

The Working with Messages docs include an explanation of how to identify the path to any message element using the Debug sidebar. This is what Steve was referring to in his reply.

I have been trying to follow thw 'working with messages' documentation as mentioned repeatedly in this thread, and failing miserably. when I copy the path to the 'temp' it gives me 'payload.observations[0].uk_hybrid.temp', but when i run the function it gives the 'cannot read property 'uk_hybrid' of undefined.
please can someone tell me the correct path for this ?

In a function, you need to be explicit - include msg.

e.g. msg.payload.x.y.x