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

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:

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

source location

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.

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.

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.

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.

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.

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.

1 Like

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.

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.

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!!

:skull_and_crossbones:

We certainly don't want that :wink:

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

1 Like

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.

1 Like

Urm, no, never :cough: :blush:

3 Likes

I was gonna suggest exactly this but decided to wait it out ...