Fri Aug 18 2023

Fetch JSON File

Node JS100 views
Fetch JSON File

File Name: fetch-json-using-nodejs.js

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 }

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.