Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda