12

I receive a JSON as a buffer. I want to parse it into a readable or JSON object.

However, despite all techniques (JSON.stringify(), toString('utf8'), I am not able to get it done.

here is what I have so far:

enter image description here

And here is what it gives me:

enter image description here

How can I transform it into a readable something?

5
  • Please visit the help center, take the tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. Please do not post PICTURES of code Commented Mar 8, 2019 at 8:11
  • Looks like you need to use fromCharCode on the result Commented Mar 8, 2019 at 8:12
  • @mplungjan Code comming from a VM without clipboard sharing capacity. Commented Mar 8, 2019 at 8:12
  • The last one seems to be the "readable something". Seems like serialization goes wrong on the other end. Commented Mar 8, 2019 at 8:14
  • Lol :))) You need to get to the provider and find out why they send you [object Object] console.log(String.fromCharCode(91,111,98,106,101,99,116,32,79,98,106,101,99,116,93)) Commented Mar 8, 2019 at 8:16

3 Answers 3

13

Your code is working. The buffer you have is actually the string "[object Object]".

let b = Buffer.from('[object Object]', 'utf8')
console.log(JSON.stringify(b))
// {"type":"Buffer","data":[91,111,98,106,101,99,116,32,79,98,106,101,99,116,93]}

console.log(b.toString('utf8'))
// [Object object]

The problem you need to figure out is why is a buffer with that string being sent. It seems like the sender of the buffer needs to call stringify or otherwise serialize the object before sending it. Then you can turn it back to a string with toString() and use JSON.parse() on the string.

Sign up to request clarification or add additional context in comments.

Comments

4

Try

console.log(Buffer.from(val).toString());

This will convert [object Object] as a string

Comments

1

If you are comfortable with hexadecimal representation of buffers use:

Buffer.from(val).toString('hex')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.