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

106
Vistas
Parsing Json data from a file

I am building a Node JS application that reads file that contains array of Json objects, and displays it on a table. I need to parse the JSON data array. sample json data:

[{"name":"Ken", "Age":"25"},{"name":"Pulsar", "Age":30}]

I have used the following to read from file and pass it into another json object:

const fileRead = fs.readFileSync("/Users/mken/Desktop/Node JS/DATA-TABLE/public/files/data.json", (err, data)=>{
console.log(JSON.parse(data))
return JSON.parse(data);

});

console.log(fileRead)

The expected output is array of JSON objects. However, when I console.log the fileRead, I do not get intended output:output

I further intend to iterate through the data read above and pass it to a JSON object:

const data = {headers:["Name", "Age"], rows: fileRead.foreach((row)=>{return row.name, row.age];})}

Please check and advise.

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

const fs = require('fs');

// Thanks to: https://nodejs.dev/learn/reading-files-with-nodejs
try {
  const data = fs.readFileSync('test.txt', 'utf8');
  console.log(data);
  const json = JSON.parse(data);
  console.log(json);
} catch (err) {
  console.error(err);
}
about 4 years ago · Santiago Gelvez Denunciar

0

the issue was with encoding. I passed utf-8 to filereadsync as second parameter, and it worked. Thank you

about 4 years ago · Santiago Gelvez Denunciar

0

If I do your task, I'll save data on a table, then I'll paste data to a json file and I'll read the file like your code.

I do that because I want to control the format of the JSON file. If you save a manual file in JSON, there are many ways you have problems with formatting JSON.

I hope my answer is helpful. Good luck!

 

about 4 years ago · Santiago Gelvez 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