varul jain
varul jain

Reputation: 334

Club different MQTT Topic data into one JSON string in node red

How can I club all the MQTT Topic data in one JSON String I was able to get JSON string for the individual topic only like this

 "{"time":1549737900821,"payload":"1997.32","topic":"RotateZ"}"
  {"time":1549737900821,"payload":"1954.32","topic":"RotateY"}"

but I want to display all the topic data in one JSON string only for example

"{"time":1549737900821, "RotateZ":"1997.32", "RotateY":"1954.32"}"

I am using mentioned below code in function node

var topic = msg.topic;
var d = new Date();
var t = d.getTime();
payload = {"time":t, "payload" : msg.payload ,"topic": topic }
msg.payload = payload;
return msg;

what modification will help me to make it work? Any suggestion on this will be a great help

Upvotes: 0

Views: 924

Answers (1)

Karel Sniper Žák
Karel Sniper Žák

Reputation: 200

Try Join node. Set Manual, Combine each msg.payload to create a key/value object using the value of msg.topic as the key. On output add timestamp in simle function like this :

var d = new Date();
msg.payload.time = d.getTime();
return msg;

Upvotes: 2

Related Questions