Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!