through2.obj makes an object stream (or a stream in object mode). Through an object stream objects flow, not buffers. What you get is not a buffer, but an object obj. That is why its toString method gives [object Object]. Perhaps what you are looking for is in obj.contents?
W3Schools
w3schools.com โบ nodejs โบ met_buffer_tostring.asp
Node.js buffer toString() Method
The toString() method returns the buffer object according to the specified encoding.
01:54
StringBuffer (Technique of the Week) - YouTube
00:58
Buffer toString method to print the buffer values using Nodejs ...
Using the Java StringBuffer class - YouTube
16:40
Difference Between String,โ String Buffer and String Builder ...
14:40
String, String Buffer & String Builder in Java (Tutorial #6) - YouTube
38:26
StringBuffer Class & Methods in Java with Example - YouTube
Node.js
nodejs.org โบ api โบ buffer.html
Buffer | Node.js v26.10.0 Documentation
Returns a new ReadableStream that allows the content of the Blob to be read as a stream of UTF-8 decoded strings. It is equivalent to piping blob.stream() through a TextDecoderStream set up with UTF-8. ... The content-type of the Blob. Once a <Blob> object is created, it can be sent via MessagePort ...
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer. The ArrayBuffer() constructor creates a new ArrayBuffer of the given length in bytes. You can also get an array buffer from existing data, for example, from a Base64 string or from a local file.
Top answer 1 of 3
359
You need to stringify the json, not calling toString
var buf = Buffer.from(JSON.stringify(obj));
And for converting string to json obj :
var temp = JSON.parse(buf.toString());
2 of 3
8

Kindly copy below snippet
const jsonObject = {
"Name":'Ram',
"Age":'28',
"Dept":'IT'
}
const encodedJsonObject = Buffer.from(JSON.stringify(jsonObject)).toString('base64');
console.log('--encodedJsonObject-->', encodedJsonObject)
//Output --encodedJsonObject--> eyJOYW1lIjoiUmFtIiwiQWdlIjoiMjgiLCJEZXB0IjoiSVQifQ==
const decodedJsonObject = Buffer.from(encodedJsonObject, 'base64').toString('ascii');
console.log('--decodedJsonObject-->', JSON.parse(decodedJsonObject))
//Output --decodedJsonObject--> {Name: 'Ram', Age: '28', Dept: 'IT'}
Top answer 1 of 2
1
You should pass the encoding to the .toString() function like in thew following example:
foo.toString('utf8');
2 of 2
0
Try this :
let data = {"buffer": {
"type": "Buffer",
"data": [
8,
4,
18,
32,
203,
243,
203,
190,
254,
80,
185,
227,
225,
187,
114,
219,
243,
87,
83,
254,
248,
95,
139,
1,
26,
198,
149,
158,
30,
44,
146,
237,
166,
70,
199,
9,
26,
32,
138,
251,
102,
14,
230,
244,
102,
250,
142,
53,
210,
190,
9,
23,
169,
98,
14,
156,
148,
99,
168,
204,
145,
108,
191,
223,
189,
58,
202,
61,
52,
254
]
}}
let newstr = JSON.stringify(data);
console.log(newstr)
console.log(JSON.parse(newstr).buffer.data)
Joshtronic
joshtronic.com โบ 2022 โบ 11 โบ 27 โบ convert-a-buffer-to-a-string-in-nodejs
Convert a buffer to a string in Node.js - Joshtronic
November 27, 2022 - The buffer is effectively just an array of integers that represent the data that it contains. To get a string back out of the buffer, simply use .toString():
Stack Overflow
stackoverflow.com โบ questions โบ 48980375 โบ how-to-properly-turn-buffer-object-into-string
How to properly turn buffer object into string?
February 26, 2018 - The data that is received is a buffer, the object that comes out of it is suppose to look something like this : {name:"usersname"}. using .toString() on the buffer, gives me name=username (in string format obv)
Medium
medium.com โบ @techsuneel99 โบ how-to-convert-a-buffer-to-json-in-node-js-4fa9ef98160
How to convert a Buffer to JSON in Node.js ? | by Suneel Kumar | Medium
December 31, 2023 - In this example, a Buffer is created ... object. Finally, the JSON representation is logged to the console using JSON.stringify. Note that this example creates a simple JSON structure with an array (data) to hold the buffer elements. Depending on your specific use case, you might want to structure the JSON differently or include additional information. Adjust the code accordingly based on your requirements. Buffer.values() returns an iterator ...
Bureau of Economic Geology
beg.utexas.edu โบ lmod โบ agi.servlet โบ doc โบ detail โบ java โบ lang โบ StringBuffer.html
java.lang Class StringBuffer
Converts to a string representing the data in this string buffer. A new String object is allocated and initialized to contain the character sequence currently represented by this string buffer. This String is then returned.
