Reading a JSON File in Node.js

To read a JSON file in Node.js, you can use the built-in fs (file system) module as you would to read any text file and then use the JSON.parse() method to parse the file's contents into a JavaScript object. In this Node.js Read JSON File Example, we read a JSON file into a memory string using the fs.readFile() method. Click Execute to run the Node.js Read JSON File Example online and see the result.
Reading a JSON File in Node.js Execute
const fs = require('fs');

fs.readFile('data.json', 'utf8', (err, jsonString) => {
    if (err) {
        console.error('Error  the file:', err);
        return;
    }
    try {
        const data = JSON.parse(jsonString);
        console.log(data);
    } catch (err) {
        console.error('Error parsing JSON string:', err);
    }
});