How to Get Data from Json File in Node Js Tutorial

Share this Article
Reading Time: 5 minutes
552 Views

Inside this article we will see the concept of get data from json file in node js. If you are wondering to read a json file and get data from it in node js then this tutorial will help you.

We will cover all possible ways to read a json file in node js inside this article. You will get more and more from this node js tutorial. Reading and getting json data in node js is a very simple process.

Node JS is an environment where we can deploy and execute javascript based applications. In this article we will read json data from file.


Premium Courses Running Out, Grab Now!

Master the Coding Skills to Become an Expert in Web Development. A lifetime of knowledge at your fingertips.

Enroll in your favourite web development courses risk-free at $4 only.


Learn More –

Let’s get started.


Node Js Application Setup

Please make sure you have node and npm both installed. To check available versions use these commands.

To check node version

$ node -v

To check npm version

$ npm -v

Create a folder with any name say readjson. Open this folder into terminal or command prompt.

Next, we need package.json file. Run this given command into terminal

$ npm init -y

The given command will auto generate package.json file with default values.

Next we need to create a file say server.js into node application.


Prepare JSON Data

Create a file data.json into your node application. data.json is a sample file to understand. You can take your json file.

Here is the sample code for data.json file.

[
    {
        "id": 1,
        "name": "Sanjay Kumar"
    },
    {
        "id": 2,
        "name": "Ashish Kumar"
    },
    {
        "id": 3,
        "name": "Vijay Rohila"
    },
    {
        "id": 4,
        "name": "Dhananjay Negi"
    }
]

Method #1 Read JSON File Data

Open server.js file and write this following code into it.

const users = require("./data"); // Including data.json

console.log(users);

Application Testing

Open project terminal and type this command.

$ node server.js

Method #2 Read JSON File Data

Open server.js file and write this following code into it.

const fs = require('fs');

let rawdata = fs.readFileSync('data.json');
let users = JSON.parse(rawdata);

console.log(users);

Application Testing

Open project terminal and type this command.

$ node server.js

Method #3 Read JSON File Data

Open server.js file and write this following code into it.

const fs = require('fs');

fs.readFile('data.json', (err, data) => {
    if (err) throw err;
    let users = JSON.parse(data);
    console.log(users);
});

Application Testing

Open project terminal and type this command.

$ node server.js

We hope this article helped you to learn How to Get Data from Json File in Node Js Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.