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.
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;
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.
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.
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"
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 ?