Hi,
I am new to NodeRed and I'm having trouble with parsing and generating msg. I have a nested object message that I am tried to parse into a new msg. The message is coming from an MQTT In node. In the debug window I see:
Here is the payload from the MQTT IN Node:
object
topic: "sensor/pushnotification"
payload: "
{"topic":"sensor.basement_hall.battery","payload":"Battery Low Warning","subtext":"10/9/2022
09:55:47 AM","count":null,"percent":null}"
qos: 2
retain: false
_msgid: "7042e909d390b98f"
I run that msg through a JSON node and here is the msg output:
object:
topic: "sensor/pushnotification"
payload: object
topic: "sensor.basement_hall.battery"
payload: "Battery Low Warning"
subtext: "10/9/2022 09:55:47 AM"
count: null
percent: null
qos: 2
retain: false
_msgid: "7042e909d390b98f"
I want to generate a new msg with some of the nested object fields and I can't seem to figure out how to reference them in a function. For example :
var sTopic = msg.topic.payload.topic;
var sPayload = msg.topic.payload.payload;
var sSubtext = msg.topic.payload.subtext;
msg = {
payload : sPayload,
topic : sTopic,
subtext : sSubtext
}
return msg;
That results in:
function: (error) TypeError: Cannot read properties of undefined (reading 'subtext')