What is JSON?
JSON (JavaScript Object Notation) is a simple format used to store and transport data. The data is presented as a collection of name/value pairs. JSON is easy for both humans and machines to read and write. JSON is not limited to JavaScript and can be used in other programming languages as well. In Node.JS, there are various ways approaches you can take to read or write JSON files. This article explores how you can use the fs module and third-party NPM packages to read and write these files. By the end, you should be able to convert data to and from the JSON format and read JSON files both synchronously and asynchronously.
Understanding JSON
To get started, let us first understand how JSON organizes data. Consider the following JSON code:
let name = {
“firstName”: “Jane”,
“lastName”: “Doe”.
“age”: “20”
}
The above code illustrates how you can use JSON to store information. The code is referencing a variable, “name” whose value is an object containing information.
Most of the time, you will need to convert a JavaScript object to JSON. To do this, you can pass the JavaScript object to JSON.stringify()
.
JSON.stringify({
firstName: “Jane”,
lastName: “Doe”.
age: “20”
})
To access the information, you can use dot notation to refer to the value you need. For instance, to get the first name you can use, name.firstName
.
To convert a JSON file back to a JavaScript object, use JSON.parse()
.
JSON.parse({
“firstName”: “Jane”,
“lastName”: “Doe”.
“age”: “20”
})
How to Read JSON Files in Node.js
Node.js has a global require function and the built-in fs module that can be used to read and write JSON files. The following sections explore how you can use these two methods when working with JSON.
Method-1: Using fs module
The fs module enables interaction between the file systems and Node.JS applications. To use it, first import it into your application.
const fs = require('fs');
The fs module has a synchronous and asynchronous version for its functions. The synchronous versions execute sequentially and therefore block the event loop until they complete executing. Asynchronous versions, on the other hand, use non-blocking callback APIs. To read JSON files using the fs module, you can use fs.readFile
or fs.readFileSync
.
The readFile
method asynchronous reads the entire contents of a file. The basic syntax of this method is:
fs.readFile(path, options, callback);
The path is the URL of the JSON file, options are optional arguments like encoding and callback is the function called after the operation ends. The callback function accepts an error object as the first argument and a JSON object as the second argument. Consider the following code that reads a file named users.json
:
const fs = require('fs');
fs.readFile('./users.json', 'utf8', (err, jsonData) => {
if(err){
console.log(err);
return;
}
console.log(JSON.parse(jsonData));
})
In the above code, you are reading the users.json
file and passing in utf-8 encoding as an optional argument. When the file is read successfully, turn the JSON data into a JavaScript object using JSON.parse
. This makes it possible o access the data received. If any errors occur, the callback function logs them on the console and terminates the function.
You can also use the readFileSync
method to read JSON files. Below is the syntax for fs.readFileSync
fs.readFileSync(path, options);
Similar to fs.readFile
, the path is the URL to the JSON file and the options represent optional arguments. However, fs.readFileSync
does not have a callback API and instead returns the contents of the file after it executes. For large files, your app can pause for a long time while waiting for the JSON file to be read which is not optimal.
const jsonData = fs.readFileSync("./users.json");
console.log(JSON.parse(jsonData));
To handle errors, you can use the try…catch block.
try {
const jsonData = fs.readFileSync("./users.json");
console.log(JSON.parse(jsonData));
} catch (err) {
console.log(err);
return;
}
Method-2: Using require Function
When working with static files that do not change often like configuration files, you can use the require function. This function synchronously loads the JSON file into your application. When a JSON file is loaded, require()
caches its content in the module it's called in and every time you need to read the JSON file, it is restored from the cache that was created. You should be careful with this method. If using JSON files that keep changing, your changes might not be reflected in your application since the cached content is what is used. Below is an example of how you can use require()
const users = require(“./users.json”)
console.log(users)
Read JSON file using third-party NPM Libraries
There are a variety of npm packages you can utilize to read data from JSON files. This section looks at two of the most popular packages; jsonfile and fs-extra.
Method-1: Using jsonfile
jsonfile
seeks to reduce the amount of code needed to read JSON. One of its features is that it comes with the ability to serialize and deserialize JSON without additional code. To install jsonfile
, run the following command:
npm i jsonfile
You can use jsonfile as shown in the code below to read JSON:
const jsonfile = require("jsonfile");
jsonfile.readFile(“./users.json”, (err, data) => {
if (err) {
console.log(err);
return;
}
console.log(data);
});
You can also use promises instead of a callback function.
const jsonfile = require('jsonfile')
jsonfile.readFile(“./users.json”)
.then(data => console.log(data))
.catch(err => console.log(err))
Method-2: Using bfj
According to the bfj documentation, bfj was created to handle large datasets of JSON. Through bfj
, you can read large JSON data without blocking the event loop or exhausting the memory. To read a JSON file from the system, you use the read function.
To get started, install the bfj package from npm.
npm i bfj
To read the file, here is a simple example that uses promises to read users.json.
const bfj = require('bfj');
bfj.read(“./users.json”)
.then(data => {
console.log(data)
})
.catch(err => {
console.log(err)
});
Conclusion
Through this article, you learned how you can read JSON files in Node.JS. The fs
module offers the readFile
and readFileSync
methods that allow you to read data asynchronously and synchronously respectively. The require()
function, on the other hand, enables you to load JSON files to the module you are working in. This function is however suited for files that don’t change much.
Further, the article explored jsonfile
and bfj
npm packages and gave examples of how they can be used to read JSON. Third-party packages seem to offer more functionality than built-in solutions. However, before choosing them for your application consider what introducing new dependencies means for your app.
Most of the methods discussed in this article, also have methods that enable you to modify a JSON file by writing to it. The fs
module for instance has the readFile
and readFileSync
functions.
Additional Resources
Check out JSON on MDN Docs
Learn more about the Node.JS file system