I am trying to extract one part of a mqtt message and use it as a payload in a node.
The incoming payload is {time:"00:02"} and i would like to use just the 00:02 for my new payload. I tried the change node by setting SET msg.payload to msg.payload.time and the debug comes back as undefined.
the first thing to check is whether the message is a JSON string, or a JavaScript object - you can do that with the Debug sidebar. If its a JSON string, then pass it through a JSON node to parse it to a JavaScript object and then you'll be able to access the individual parts.
I cant seem to figure out how to extract just the time "20:00" from this and display it in a text node.
Can anyone give me some sort of example or help with this please?
You need to know a little about javascript to fully understand this.
The data you are seeing is a string ...
but the format of that string looks like JSON (JSON is a string representation of a JS object)
If it were an object you could access the value simply by accessing msg.payload.time and you would get 20:00
I recommend you read up a bit on javascript objects and JSON - but to get you moving, either pass the value through a JSON node or set the MQTT node to return a JS object.
If it is of length 27 then it has not got what you previously showed (which was length 14). Feed the string into a debug node and also into a JSON node. When the data is valid it will be converted to an object by the JSON node, but if the longer invalid string appears you will get the invalid json message from the JSON node and can look in the debug node to see what it is.
Im lost. I have tried all of these suggestions. I even went to node-red guide and read chapter 3. I followed their examples to the letter and i am still not getting it. I even went to the node red description and copied and imported to a new flow as follows:
I can see that the quotes round analyse and value are 'smart' quotes rather than normal double quotes. Look carefully at the debug output and you will see that they are different at each end and different to the surrounding double quotes. Where are those coming from?
ok, i gotcha. I had to read it over a hundred times or so before is sunk in. I will work backwards from here and see if get anywhere. I changed the quotes and published the message and it is working now.