How to Add element to msg.payload

#1

If I have

msg.payload = {
    Element1 :"One",
    Element2 : "Two",
    Element3 : "Three"  
}
return msg;

The output is: {"Element1":"One","Element2":"Two","Element3":"Three"}
But now, If I want to add a new element, how do I do it?

For the output like this: {"Element1":"One","Element2":"Two","Element3":"Three","Element4":"Four"}

Thank you.

0 Likes

#2

try a google search 'json opject adding element to' and see what you find.

0 Likes

#3

Understanding javascript objects is very important for using Node-RED.

I found the following tutorial very helpful
https://www.w3schools.com/Js/js_objects.asp

2 Likes

#4
msg.payload = {
    Element1 :"One",
    Element2 : "Two",
    Element3 : "Three"  
}

msg.payload["Element4"]="Four"
return msg;

Thanks to all

1 Like