How to parse JSON files in Node.JS ?

Quick tip using fs:

let data = fs.readFileSync("myFile.json", 'utf8');
console.log(typeof JSON.parse(data));

There is another way to do this that requires less effort. Using require directly, like so:

const data = require("myFile.json");
console.log(typeof data);
Or you can create a function like this with fs also:
function loadAlgorithms(fName) {
    let data = fs.readFileSync(fName, 'utf8');
    return JSON.parse(data);
}
Hope it helps.

Leave a Comment

Your email address will not be published. Required fields are marked *