Best ways to read local json file in Nodejs application with example

You can also check other posts on npm command deprecate option is deprecated In this tutorial, multiple ways to read local JSON files in Nodejs application

  • using required js
  • fs module readFile

Let’s declare a local JSON file in the nodejs project emp.json

{
  "id": 1,
  "name": "John",
  "salary": 5000
}

easy way to read local json file with require to function in nodejs

This is a simple and easy way to read using require function

const json = require("./emp.json");
console.log(json);

It directly reads the file as an object from the local file. Notes:

  • It is simple and easy to read
  • During application running, It only read the content on the file but does not read the modified content. You need to restart the server to reload modified content from the local JSON file.

Nodejs provides a built-in fs module to work with file systems. Reading a file in JSON involves two ways

  • synchronous with readFileSync
  • asynchronous with readFile

Let’s see code with nodejs fs module read file example

How to read local json as a string in nodejs

fs module has readFileSync method

syntax

fs.readFileSync( filepath, fileoptions )

Parameters are the path of a file and options are optional configurations like encoding details.

const fs = require("fs");
const data = fs.readFileSync("./emp.json", "utf8");
console.log(data);

Output

{
"id": 1,
"name": "John",
"salary": 5000
}
{ id: 1, name: 'John', salary: 5000 }

Here are the following steps to read the local JSON file

  • First, Import the fs module into your js file readjson.js

  • call the readFileSync method with the path of the file and encoding details

  • log the JSON content into the console

Notes with this approach

  • It reads during code execution and changes to the JSON file are reloaded and read as expected
  • It is synchronous of reading the file, Performance is not good, the current thread waits for the reading to complete.
  • It is not suitable for reading the large file as file content is read into memory.

Asynchronous reading local json file using the nodejs fs module

This is an asynchronous way of reading JSON files from the file system.

  • Import the fs module into code using require function
  • nodejs fs filesystem module has the readFile method which asynchronously read the path of the local JSON file and returns callbacks
  • Callbacks contain two parameters, One is error and another is a string object, the error object contains an error if an error occurs, string objects read the JSON content into a string.
  • Finally, parse string object to convert into of type Object using json.parse() method
  • finally, convert the string into an object and wraps parsing error results in try and catch block
const fs = require("fs");
fs.readFile("./emp.json", "utf8", (err, jsonObject) => {
  if (err) {
    console.log("Error callback:", err);
    return;
  }
  try {
    console.log(jsonObject); // string object
    const jsonData = JSON.parse(jsonObject);
    console.log(jsonData); //object type
  } catch (err) {
    console.log("Error while parsing JSON string data:", err);
  }
});

Notes:

  • This is the best and real-time way of reading files
  • Current thread will not wait for the reading file operation to be completed.

Conclusion

To Sum up, You learned multiple ways of reading local JSON files in the nodejs application.