Converting buffer to string

Hi,

I am trying to convert a buffer data to string data

Buffer data i am pushing in as follows,
[123,34,100,97,116,97,34,58,34,52,49,49,48,48,52,34,44,32,34,112,111,114,116,34,58,50,44,32,34,116,105,109,101,34,58,34,105,109,109,101,100,105,97,116,101,108,121,34,125,0]

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.

https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_buf_tostring_encoding_start_end

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.

2 Likes

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