# Publish json data through mqtt

**URL:** <https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895>\
**Category:** General\
**Created:** [18 July 2023 05:41 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895 "2023-07-18T05:41:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PRABHAKAR82](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@PRABHAKAR82](https://discourse.nodered.org/u/PRABHAKAR82)\
**Post date:** [18 July 2023 05:41 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/1 "2023-07-18T05:41:38Z")

</div>

```auto
{
    "w": [
        {
            "tag": "RESET1",
            "value": 0
        }
    ],
    "ts": "2017-12-28T12:22:21+0000"
}

```

I have to publish this data through mqtt but in this code value is static and I want to make value dynamic.  
value tag data will come from numeric input or slider. How i make value dynamic?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [18 July 2023 06:04 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/2 "2023-07-18T06:04:05Z")

</div>

Have you tried using variables inside the object, like this?

```auto
var variable = "Hello";
var staticValue = "World";

var jsonObject = {
  staticKey: staticValue,
  variableKey: variable
};

var jsonString = JSON.stringify(jsonObject);
msg.payload = jsonString;
return msg;

```

Then publish the jsonString via MQTT.

---

<div class="post-metadata">

**Author:** ![PRABHAKAR82](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@PRABHAKAR82](https://discourse.nodered.org/u/PRABHAKAR82)\
**Post date:** [18 July 2023 06:20 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/3 "2023-07-18T06:20:11Z")

</div>

I tryed this but not working,

```auto
var variable =msg.payload;
var staticValue = "Tag1";

var jsonObject = {
    staticKey: staticValue,
    variableKey: variable
};

var jsonString = JSON.stringify(jsonObject);
return jsonString;

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [18 July 2023 06:26 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/4 "2023-07-18T06:26:32Z")

</div>

Please try this corrected version. (Sorry - I was typing too fast.)  
The issue was the function needs to return an object, not a string.

```auto
var variable = msg.payload;
var staticValue = "World";

var javascriptObject = {
    staticKey: staticValue,
    variableKey: variable
};

var jsonString = JSON.stringify(javascriptObject);
msg.payload = jsonString;
return msg;

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [18 July 2023 06:58 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/5 "2023-07-18T06:58:23Z")

</div>

Did you manage to try the revised version?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 July 2023 07:20 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/6 "2023-07-18T07:20:44Z")

</div>

In fact it is not necessary to convert the javascript object to a JSON string before passing to MQTT, the MQTT node will do that for you. So this should work

```auto
var variable = msg.payload;
var staticValue = "World";

var jsonObject = {
    staticKey: staticValue,
    variableKey: variable
};

msg.payload = jsonObject;
return msg;

```

Though calling it jsonObject is a misnomer as it is actually a javascript object. It is not JSON, as JSON is a string.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [18 July 2023 07:34 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/7 "2023-07-18T07:34:15Z")

</div>

Thanks Colin for pointing out my misnomer - which I have corrected in my listing above.

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [18 July 2023 09:13 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/8 "2023-07-18T09:13:17Z")

</div>

In the never ending debate function node or not, here using change node and jsonata you can get the same result.

 ![Screenshot 2023-07-18 at 11.12.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33757368460e81f031d3f4b7f5619dfa41a0bd51.png)

---

<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:** [1 August 2023 09:13 UTC](https://discourse.nodered.org/t/publish-json-data-through-mqtt/79895/9 "2023-08-01T09:13:39Z")

</div>

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