Parsing MQTT payload into Tago JSON

Hello,

Can you help me parse the payload below into Tago JSON format?
This is what i’m receiving in Live inspector.

21:16:36:
[POST] MQTT Request:
“From: ::ffff:10.0.2.48 - Content-Type: JSON - Token Ending: d38c5”
“From: ::ffff:10.0.2.48 - Content-Type: JSON - Token Ending: d38c5”
21:16:36:
Raw payload:
{ “acc”: { “x”: “5”, “y”: “0”, “z”: “1026” }, “mag”: { “x”: “50”, “y”: “-10”, “z”: “-35” }, “gyr”: { “x”: “122”, “y”: “-183”, “z”: “61” }, “env”: { “l”: “2880”, “r”: “64”, “p”: “99357”, “t”: “25.141” } }
{
“acc”: {
“x”: “5”,
“y”: “0”,
“z”: “1026”
},
“mag”: {
“x”: “50”,
“y”: “-10”,
“z”: “-35”
},
“gyr”: {
“x”: “122”,
“y”: “-183”,
“z”: “61”
},
“env”: {
“l”: “2880”,
“r”: “64”,
“p”: “99357”,
“t”: “25.141”
}
}

Hi @Nicolas
There are several ways to parse your data. But it depends on what you want to have as result of the parse.

Can you explain what would be the result of this parse?

Hello,

I want to create a dashboard in Tago. First I was able to onboard the device and connect to MQTT tago broker, now i want to create a dashboard with the data received, basically storing this data into buckets so i can create a dashboard.

Please help me with the parsing.

Thanks,

Nicolas

Hi @Nicolas,
A very simple parser would be like this. Give it a try and change what you need.

if (payload && (!Array.isArray(payload)) || !payload[0].variable) {
  if (Array.isArray(payload)) payload = payload[0];

  const acc = { ...payload.acc };
  const mag = { ...payload.mag };
  const gyr = { ...payload.gyr };
  const env = { ...payload.env };

  const serie = new Date().getTime(); // generates a unique serie for this batch of data.

  payload = [
    { variable: "acc_x", value: Number(acc.x), serie },
    { variable: "acc_y", value: Number(acc.y), serie },
    { variable: "acc_z", value: Number(acc.z), serie },
    { variable: "mag_x", value: Number(mag.x), serie },
    { variable: "mag_y", value: Number(mag.y), serie },
    { variable: "mag_z", value: Number(mag.z), serie },
    { variable: "gyr_x", value: Number(gyr.x), serie },
    { variable: "gyr_y", value: Number(gyr.y), serie },
    { variable: "gyr_z", value: Number(gyr.z), serie },
    { variable: "env_l", value: Number(env.l), serie },
    { variable: "env_r", value: Number(env.r), serie },
    { variable: "env_p", value: Number(env.p), serie },
    { variable: "env_t", value: Number(env.t), serie },
  ]
}