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

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

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 Denunciar

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 Denunciar

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 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