Converting buffer to string


I am trying to convert a buffer data to string data

Buffer data i am pushing in as follows,

the data displays when it converted to string as below,
{"data":"411004", "port":2, "time":"immediately"}

how should i need to proceed in node red?

1 Like

You can use a function node for the conversion. Have a look at NodeJS' Buffer.toString() method.

Actually I think it is myBuffer.toString() that you want. Try that and feed the result to a debug node to check it is ok. If it is then you should be able to send that through a JSON node to convert it to a js object which will make life even easier.
Where is this coming from? There may be other alternative solutions.

Ah, sorry. That's what I meant. I corrected my post. :see_no_evil:

@kuema @Colin thanks for the replay i did convert this buffer data into string
var b=Buffer.from(msg.payload);
var s=b.toString();
var out=s;
msg.payload= out;
return msg;

but i need it convert it to json string while i am using a json node its giving the data as follows,
""{"data":"411004", "port":2, "time":"immediately"}\u0000""
how to eliminate the "" from this frame?

i am sending a data from my system platform through MQTT broker to my gateway . it is sending a buffer string data thats why i am trying to convert it in JSON on node red

Far too complicated. :sweat_smile:

If your payload is already a Buffer, then the following should suffice:

msg.payload = msg.payload.toString();
return msg;

That will output a string, which is your JSON I suppose. Use the JSON node afterwards.

MQTT? I think this could be even simpler. You can choose the output format of the MQTT in node. Make sure it's set to 'a parsed JSON object', not buffer.


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