Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Why would someone use JSON.parse(JSON.stringify()) on a Buffer?

I have some existing code that is working with Buffer objects.

let dataObject = JSON.parse(JSON.stringify(data));

Which seems to me like a no-op, ie it doesn't do anything. That's not correct though, as if I replace it with:

let dataObject = data;

The code fails. I've some some investigation and:

  • typeof data is object
  • data.constructor.name is Object
  • data when logged is <Buffer>... (long stream of bytes)

What does JSON.parse(JSON.stringify(buffer)) do? Is there a better or clearer way of doing this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The buf.toJSON() method has a misleading name because it returns a JavaScript object, while JSON is actually a string data format. However, it returns a representation of the Buffer and the data it contains, so you can replace this:

let dataObject = JSON.parse(JSON.stringify(data));

With this cleaner code:

let dataObject = data.toJSON();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!