Payload binary to Json object

Hi everyone, i have a problem to convert data in binary to JSON Object.

I read this binary buffer from websocket:

This is the same in string payload

when i use a function with msg.payload= msg.payload.toString(); i get the same string but with double quotation marks, the JSON node here dont works

I m stuck from this, because i dont get the way to parse this and get an object, later i want to parse to handle a bridge with modbus tcp, but please help me with this, because i test some solutions on the forum and no ones works :confused:

Regards.

Did you try:

msg.payload = JSON.parse(msg.payload.toString())
1 Like

Yes i try that and dont work , this is the result

Maybe instead of a picture of your screen you could share the actual buffer data?

The debug window shows a "Copy value" button on every message.

In the forum click the </> button and paste your copied data.

Spot the difference:

In short, your data has quotes around it making it invalid JSON.

If you can affect the thing sending the data, you should correct it there.

Failing that, you need to strip off the outer quotes before parsing.

e.g...

  if (msg.payload.length >= 2 && msg.payload.startsWith('"') && msg.payload.endsWith('"')) {
    msg.payload = msg.payload.slice(1, -1)
  }
2 Likes