This tutorial will help you to read JSON file using Node.js uses readFile and readFileSync functions of the jsonfile module.

Installing Node Module

For this tutorial, we are using jsonfile npm module. So first you need to install jsonfile module on your system Next create a sample json file for running few examples. You can use your own json file. Add the follwoing snippte: Save file content and close it. Next, we will run a few examples to read JSON file created above.

Option #1: Read JSON File with Nodejs

In the above step, I have created a sample JSON file. Now create ReadJsonFile.js and add the following content. You need to change employee.json with your JSON file name. Filename: ReadJsonFile.js

Now run the nodejs script using following command.

Option #2: Read JSON File (readFileSync)

Alternatively, you can use readFileSync function to read json file content. Create a ReadJsonFileSync.js file with following content. You can read here about the differences of readFile and readFileSync function in Jode.js. Filename: ReadJsonFileSync.js

Now run the nodejs script using following command.