How to Add element to msg.payload

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.

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

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
msg.payload = {
    Element1 :"One",
    Element2 : "Two",
    Element3 : "Three"  
}

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

Thanks to all

7 Likes

way to contribute

3 Likes

Plz, this is not usefull so ...

@biishop if you have a question, please open your own thread. Add detail related to your problem.

This post is over 2 years old.

Closing this thread as the answer by @onire77 is correct.