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

193
Visualizações
How to parse such data?(parse json array)

help me pls . I write several arrays to a txt file, and then I want to parse them, what am I doing wrong

fs.readFile("./data.txt", "utf8", 
function(error,dataRes){
  console.log('Читаю данные')
  if(error) throw error; 
  data = JSON.parse('['+dataRes+']'.replace(/\]\[/g,'],['));
  console.log(data)
  createitem();
});

data.txt(example)

[{"update":"Сегодня, 14:44"},{"update":"Сегодня, 14:43"},{"update":"Сегодня, 14:31"}][{},{},{},{"link":"https://www.doska.by//msg/animals/cats/maine-coon/bdnkh.html"}]
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

this works but you really should fix the data.txt

const dataRes = `[{"update":"Сегодня, 14:44"},{"update":"Сегодня, 14:43"},{"update":"Сегодня, 14:31"}][{},{},{},{"link":"https://www.doska.by//msg/animals/cats/maine-coon/bdnkh.html"}]`

const obj = JSON.parse(`[${dataRes.replaceAll("][","],[")}]`).flat(); // remove .flat() if you want two arrays
console.log(obj)

about 4 years ago · Santiago Trujillo Relatório

0

Your problem is this string:

'['+dataRes+']'.replace(/\]\[/g,'],[')

The replace call is only applied to ']', not to '['+dataRes+']'. It tries to replace all occurrences of ][ in the string ']'.

You can fix your error with parenthesis:

fs.readFile("./data.txt", "utf8", 
function(error,dataRes){
  console.log('Читаю данные')
  if(error) throw error; 
  data = JSON.parse(('['+dataRes+']').replace(/\]\[/g,'],['));
  console.log(data)
  createitem();
});

This replaces all occurrences of ][ in the string '['+dataRes+']'.

Example:

const dataRes = `[{"update":"Сегодня, 14:44"},{"update":"Сегодня, 14:43"},{"update":"Сегодня, 14:31"}][{},{},{},{"link":"https://www.doska.by//msg/animals/cats/maine-coon/bdnkh.html"}]`;

const data = JSON.parse(('['+dataRes+']').replace(/\]\[/g,'],['));

console.log(data);

about 4 years ago · Santiago Trujillo Relatório

0

you have to fix data.txt

var dataTxtStr='[{"update":"Сегодня, 14:44"},{"update":"Сегодня, 14:43"},{"update":"Сегодня, 14:31"}][{},{},{},{"link":"https://www.doska.by//msg/animals/cats/maine-coon/bdnkh.html"}]';

var dataTxt= JSON.parse(dataTxtStr.replaceAll("}][{","},{"));

about 4 years ago · Santiago Trujillo Relatório
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