Invalid JSON string with MQTT node

I am using the "MQTT in" node to retrieve data on a lorawan broker. Data is received in stringBase64 format. I want the node to output a json object. For that I put the option of the node "Output" in "a persed JSON object". I get the error "Invalid JSON string" while debugging. Help me please. The other "Output" options of the node work

The option parsed JSON object means that the data from MQTT is a JSON object (string) which it will parse and output a javascript object. In other words it outputs the result of parsing the JSON object. If what you have coming in is not a JSON string then don't use that option. A JSON object is always a string.

Colin,
Thank you for your answer,
Since I cannot modify the broker's configuration, I do not have the rights to do so. Is there a node that converts a stringBase64 string into a json object?

Not directly, but you can set your MQTT node to output a string (that base64 data), pass that to a base64 decoder node (outputs a JSON string), then pass that to the json node set to always convert string to an object.

Or, if you prefer writing Javascript, something like this should work (untested):

let json = Buffer.from(payload, 'base64').toString();
msg.payload = JSON.parse(json);
return msg;

Of course, this expects there to be no conversion or parsing errors, so you'll want some data type checking and try/catch blocks...

1 Like

A JSOnata expression in a change node can decode base64 encoded strings.
You could also then use $eval($base64decode(payload)) to return a object, as long as you are sure that the data is safe to $eval(). Otherwise just $base64decode(payload) followed by a json node.

Or the node-red-node-base64 node and then the json nide

Yep, that's the one I was trying to think of...

Thank you all for your responses. I quickly tried all the solutions offered, I still haven't solved my problem. Here is the data image I get direct from the broker. I want to convert them to JSON later because I need to store them in the database. With this format it is impossible to process and store them in the database.

A JSON object is a string, I think it unlikely that you want the buffer as a string. Please show us an example of what you would like the buffer to be reformatted to.

I want to retrieve the data in standard JSON format something like this

{
    "applicationID": "1",
    "applicationName": "test_ds18b20",
    "deviceName": "arduino_uno",
    "devEUI": "1234567890123456",
    "rxInfo": [
        {
            "mac": "aa755a0048050130",
            "rssi": -57,
            "loRaSNR": 10,
            "name": "raspberry_pi",
            "latitude": 1.466860686785175,
            "longitude": 2.019478797912605,
            "altitude": 0
        }
    ],
    "txInfo": {
        "frequency": 868100000,
        "dataRate": {
            "modulation": "LORA",
            "bandwidth": 125,
            "spreadFactor": 7
        },
        "adr": true,
        "codeRate": "4/5"
    },
    "fCnt": 9,
    "fPort": 1,
    "data": "Z29vZGJ5ZQ==",
    "object": {}
}

That isn't JSON, that is a javascript object.

I don't see how the data in the buffer relates to that object. Can you explain the mapping from the buffer to the object?

The broker sends the data in buffer format. We want to convert this data to json format. Then we need to process them to store them in the database.

This is how the data looks in our lorawan server application before it is sent

What I don't see is where that data is in the buffer. The buffer starts with a newline character, followed by a DLE character, then 8cf95720.... So where is the data you want in that buffer? Is that buffer the raw data exactly as received via MQTT?

The image I showed above exactly matches the data we receive from the broker. The received payload is in hexadecimal format, so how to make this payload readable to store it in the database

Is the data you show as it comes out of the MQTT node? You said at the beginning that it was a stringBase64 format, but you are showing a buffer. Is that because you are not showing exactly what comes from MQTT or because you have told the MQTT node to output a buffer.

What does the debug show if you set MQTT node to output a string ? Can you cut and paste the whole value here ?

Sorry we made a mistake at the beginning, we thought that the data came in stringBase64 format but it is a buffer as shown in my first image. So can we convert such a format (buffer) into a json or javascript object?

Yes.

See this recent solution for similar question: Relpy to TCP -- Trouble responding with a "1" - #16 by Steve-Mcl

Is there a document specifying how the data in the buffer is formatted?

Hi,

I went through the same process, this is what I get