Club all MQTT Topic data in one json string

#1

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

0 Likes

#2

Hi @Varul92

The Join node can be used to group messages into one. If you configure it to create a key/value Object, and tell it how many different topics you are working with, it will group the messages together and send one on once it has a complete set.

0 Likes

#3

Hi @knolleary

Thanks for the update, I am able to do it with join node but the sensor data is displaying in the same line like

{"AccelX":"0.26","AccelY":"0.00","AccelZ":"0.01","RotateX":"1996.10","RotateY":"0.43","RotateZ":"1996.68"}

But is it possible to display it in the next line like this

{
"AccelX":"0.26"
"AccelY":"0.00"
"AccelZ":"0.01"
"RotateX":"1996.10"
"RotateY":"0.43"
"RotateZ":"1996.68"
}
0 Likes

#4

You said you wanted a JSON string.

The example you now give isn’t JSON (no commas between name value pairs).

But now you have the data as a javascript object, you can pass it through the JSON node to get a JSON string and then use a change node to modify the JSON into the string you want

0 Likes