Json, how can i add the msg.payload object as a variable in JSON

Hi! im new to node red, but im trying to send a payload from a sensor over to a database. this works fine using a text string. But my question is, how can i add the msg.payload variable(that holds the data from the sensor) instead of the data variable with the string Hello World?
json structure is this (added placeholders for the other variables):

msg.payload = {
'deviceId':"SensorName",
'key':"KeyData",
'protocol':"mqtt",
'data':'Hello world'
}
return msg;

let a = "some data";
msg = {
payload: a,
}
or am I looking at this backwards for what you want? otherwise afelix has a good suggestion

Use a change node and read Working with Messages.

dunno :stuck_out_tongue: but i solved it by changing it to a java script object and converting it with the json node

Shouldn't that be;

msg.payload = {
   deviceId: SensorName,
   key: KeyData,
   protocol: mqtt,
   data: 'Hello world'
}
return msg;

yes ! :slight_smile: exactly what i did, changed 'hello world with msg.payload and used a json node to convert. i guess i tried to turn the js object into json manually, the first time ^^. but thanks for reply

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.