Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda