# MQTT-in node - autodetect string, buffer and JSON object

**URL:** <https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165>\
**Category:** Feature Requests\
**Created:** [9 August 2019 10:32 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165 "2019-08-09T10:32:54Z")\
**Posts on this page:** 15\
**Page:** 1

<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:** [9 August 2019 10:32 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/1 "2019-08-09T10:32:54Z")

</div>

I am changing a lot of stuff in my flows and had to distinguish strings from objects coming from mqtt-in.

The "auto-detect (string or buffer)" does not capture JSON.

Looking in the code, the else formats it as a string. Could this be changed to trying to parse it as json first (try/catch) catch would parse it as string:

```auto
  if (isUtf8(payload)) { 
                    try { payload = JSON.parse(payload); }
                    catch { payload = payload.toString(); }
                }

```

[source location](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/io/10-mqtt.js#L418)

Just changed it in my running instance, works quite nicely. Although it would be nice to remove the option from the pulldown menu, it would have a lot of implications for existing flows. But the auto-detect/parse of/to json is quite helpful.

I am sure I am not capturing all the exceptions, but I want to eliminate the manual capturing/parsing.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 August 2019 10:48 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/2 "2019-08-09T10:48:55Z")

</div>

The reason it won't be done is exactly because "it would have a lot of implications for existing flows..." as in, it would break a lot of flows.

---

<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:** [9 August 2019 10:56 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/3 "2019-08-09T10:56:52Z")

</div>

My test is not the test to use.

I am talking about the autodetect feature, it should be possible to test whether or not the data is a string, buffer or a json object and if it is the latter send it as a json object. It is a bit redundant to use:

`mqtt-in -> json node`  
in auto-detect mode.

It is an improvement, sometimes you have to break stuff to move forward.

---

<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:** [9 August 2019 11:27 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/4 "2019-08-09T11:27:56Z")

</div>

> [@bakman2](#):
>
> test whether or not the data is a string, buffer or a json object

I don't fully understand what you mean here, I thought the data from mqtt could only be a buffer or a string. What do you mean by a json object. By definition JSON is a string.

---

<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:** [9 August 2019 11:37 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/5 "2019-08-09T11:37:42Z")

</div>

You are correct with the terminology; I mean a string formatted as an javascript object.

string vs object. The string is not useful and could be converted automagically.

 ![30](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/ded3d7731d0a32312d496f964b9915365767a289.jpeg)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 August 2019 11:38 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/6 "2019-08-09T11:38:10Z")

</div>

If the improvement breaks people then it's not much of an improvement. The option is there to output a parsed JSON object if you want.

---

<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:** [9 August 2019 11:46 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/7 "2019-08-09T11:46:30Z")

</div>

> The option is there to output a parsed JSON object if you want.

That is correct, except that I have to use multiple mqtt nodes and filter/convert its output first.  
I have reduced the number of mqtt nodes from 20+ to 1, I see it as an improvement.

---

<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:** [9 August 2019 13:00 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/8 "2019-08-09T13:00:32Z")

</div>

Possibly a solution would be to add an option to the dropdown list "Autodetect JSON or string" (or something similar) which attempts to parse a string and returns the object if successful, otherwise returns the string as is.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 August 2019 13:35 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/9 "2019-08-09T13:35:36Z")

</div>

> [@dceejay](#):
>
> If the improvement breaks people then it's not much of an improvement

Wow, I know that IoT is where digital meets physical but I didn't know that Node-RED had already progressed to the ability to break people!!

☠

We certainly don't want that 😉

---

<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:** [9 August 2019 13:44 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/10 "2019-08-09T13:44:18Z")

</div>

I think this forum is littered with the remains of those irreparably damaged by node-red.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 August 2019 14:03 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/11 "2019-08-09T14:03:08Z")

</div>

I'm sure you know IT folk who have rolled out a "simple update" on a Friday afternoon, and then had to spend the weekend undoing the mayhem.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 August 2019 17:23 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/12 "2019-08-09T17:23:31Z")

</div>

Urm, no, never :cough: 😊

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 August 2019 22:26 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/13 "2019-08-09T22:26:37Z")

</div>

I was gonna suggest exactly this but decided to wait it out ...  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/457ca80b349536fdbe7bd0dcc81cf6f8ce34a06e.gif)

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [28 November 2019 15:28 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/14 "2019-11-28T15:28:13Z")

</div>

If I may jump in here I have a terribly naive question. I have a bunch of Sonoff switches running just fine, have their status on Dashboard, etc....

What I cannot figure out is how to parse data received from a Sonoff TH10 (tasmota) device to extract variable data (like temp and humidity). I have looked around and if I've seen it explained, I didn't know it. Here is an example response:

```auto
Beechnut/Sauna-TH10/stat/STATUS10 : msg : Object
object
topic: "Beechnut/Sauna-TH10/stat/STATUS10"
payload: "{"StatusSNS":{"Time":"2019-11-28T16:11:11","SI7021":{"Temperature":22.3,"Humidity":45.5},"TempUnit":"C"}}"
qos: 0
retain: false
_msgid: "57bab8b7.eff8a8"

```

I actually can get it to work (if the MQTT respons ehas the proper data) by using

```auto
    temperature = msg.payload.StatusSNS.SI7021.Temperature;
    humidity = msg.payload.StatusSNS.SI7021.Humidity;

```

however there are a bunch of MQTT messages that cause the error msg in debug of:

```auto
function : (error)
"TypeError: Cannot read property 'SI7021' of undefined"

```

that I can't seem to stop occurring. There has to be a better method, I just don't know what it is.  
st

---

<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:** [28 November 2019 15:53 UTC](https://discourse.nodered.org/t/mqtt-in-node-autodetect-string-buffer-and-json-object/14165/15 "2019-11-28T15:53:41Z")

</div>

This is not the right topic, open a new one with your question.
