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

185
Vistas
Parse several json.stringify records from one file?

At intervals I am collecting data records that should be stored in append-mode to the end of a certain file; I am using json.stringify to render them as text. Later, when I connect to my server, I want to send all those records to the server. Doesn't look like json.parse is capable of handling more than one record; consecutive records are going to make it unhappy. Is there a well-known solution to this problem?

Here's a test program:

fs = require("fs");

const record1 = {
name: "Sarah",
age: 25
};
const record2 = {
name: "Joseph",
age: 34
};

function write() {
  var t1 = JSON.stringify(record1);
  var t2 = JSON.stringify(record2);
  fs.appendFileSync("test.json", t1);
  fs.appendFileSync("test.json", t2);
}

function read() {
  text = fs.readFileSync("test.json");
  var v1 = JSON.parse(text);
  console.log(v1);
}

write();
read();

Here's the file test.json:

{"name":"Sarah","age":25}{"name":"Joseph","age":34}

and here's the result:

Process exited with code 1
Uncaught SyntaxError: Unexpected token { in JSON at position 25

So what's the best way to come back and read those two separate records?

about 4 years ago · Juan Pablo Isaza
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