# Manipulate Json format before sending in MQTT

**URL:** <https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588>\
**Category:** General\
**Created:** [2 December 2022 01:54 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588 "2022-12-02T01:54:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jhobean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhobean/32/71782_2.png) [@Jhobean](https://discourse.nodered.org/u/Jhobean)\
**Post date:** [2 December 2022 01:54 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/1 "2022-12-02T01:54:31Z")

</div>

Hi,  
I'm new in the world of API and json format. I start my first project with some IOT device.  
I take the value of my Temperature sensor with HTTP Request on Sensecap API.

I curently use TagoIO website for my alarm and dashboard. I try to send my data to Tagoio with MQTT. The problem is they need a specific format of data.  
Now I have this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/0662021949a44cb1d048c41395f88fe4a1e46895.png)

`{"code":"0","data":[{"channel":"1","points":[{"value":"23.45","measurement_id":"4097","created":"1669950770354"},{"value":"28.5","measurement_id":"4098","created":"1669950770354"}]}]}`

I need to have something similar to this

```auto
[
  {
    "variable": "temperature",
    "value": 23.45,
    "unit": "C",
    "time": "$timestamp$",

  }
]

```

How I can manipulate the data and format it correctly? Thx for your hints.

---

<div class="post-metadata">

**Author:** ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)\
**Post date:** [2 December 2022 02:17 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/2 "2022-12-02T02:17:30Z")

</div>

use a template to put your structure together like you need. Use mustache and address the data from your message output. More tomorrow.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [2 December 2022 03:27 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/3 "2022-12-02T03:27:18Z")

</div>

If you are sending it over `MQTT` it is the `payload` that is sent.

So you would have to put all of that information in the `payload` part of the message.

eg:

```auto
msg.payload = 
    {
        "variable": "temperature",
        "value": 71,
        "unit": "F",
        "time": "$timestamp$",
        "group": "1568913302243",

    };

```

---

<div class="post-metadata">

**Author:** ![Jhobean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhobean/32/71782_2.png) [@Jhobean](https://discourse.nodered.org/u/Jhobean)\
**Post date:** [2 December 2022 03:58 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/4 "2022-12-02T03:58:52Z")

</div>

I have this originaly:  
`{"code":"0","data":[{"channel":"1","points":[{"value":"23.45","measurement_id":"4097","created":"1669950770354"},{"value":"28.5","measurement_id":"4098","created":"1669950770354"}]}]}`  
On My MQTT service, he dont recognise it.

I know 4097 is temperature sensor and 4098 is humidity.

So'll need something like

```auto
"variable": "temperature",
 "value": 23.45,
 "time": 1669950770354,

"variable": "humidity",
 "value": 28.5,
 "time": 1669950770354,

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 December 2022 07:21 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/5 "2022-12-02T07:21:30Z")

</div>

Why not an object that describes the values ?

With a change node, set a new property to a JSONata expression:

```auto
{
    "temperature":$number(payload.data[0].points[0].value),
    "humidity": $number(payload.data[0].points[1].value),
    "timestamp": payload.data[0].points[0].created
}

```

output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57cdc191ba710975ced52874066156c21a947026.png)

---

<div class="post-metadata">

**Author:** ![Jhobean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhobean/32/71782_2.png) [@Jhobean](https://discourse.nodered.org/u/Jhobean)\
**Post date:** [2 December 2022 11:37 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/6 "2022-12-02T11:37:25Z")

</div>

Seem interesting bit i'm really noob on nodered. I set this code on a function block?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 December 2022 14:37 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/7 "2022-12-02T14:37:25Z")

</div>

In a change node.

I would highly recommend to go through the extensive node red documentation, as it explains a lot of core concepts.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 January 2023 14:38 UTC](https://discourse.nodered.org/t/manipulate-json-format-before-sending-in-mqtt/71588/8 "2023-01-31T14:38:24Z")

</div>

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