Node JS

Fetch JSON File

How to fetch JSON file data using Node JS

8/18/2023
0 views
fetch-json-using-nodejs.jsJavaScript
const fs = require('fs');

const getUsers =  async () => {
    try {
        const data = fs.readFileSync('users.json', 'utf-8');

        const jsonData = JSON.parse(data);
        
        jsonData.map(usr => {
            console.log('Id:', usr.id);
            console.log('First Name:', usr.fstName);
            console.log('Last Name:', usr.lstName);
            console.log('Email:', usr.email);
        })

    } catch (err) {
        console.error(err);
    }
}

module.exports = { getUsers }
NodeJSJavaScriptFetch DataJSON

Loading comments...

Related Examples

Data Validation using Middleware and JOI
Node JS

Data Validation using Middleware and JOI

How to Connect Node.js with MongoDB using Mongoose
Node JS

How to Connect Node.js with MongoDB using Mongoose