How to read and write JSON file using Node ?
Last Updated :
07 Jan, 2025
Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. It finds extensive usage in creating APIs and microservices, catering to the needs of both small startups and large enterprises.
JSON(JavaScript Object Notation) is a simple and text-based format for exchanging data between different applications. Similar to XML, it's a commonly used method for web applications and APIs to communicate and share information.
Below are the different methods to read and write JSON files:
Note: Reading and writing JSON files in Node.js is crucial for handling configuration and data storage.
Method 1: Using require method:
A straightforward way to read a JSON file in a Node JS file is by using the `require()` method to include it.
Syntax:
const data = require('path/to/file/filename');
Example: Create a users.json file in the same directory where index.js file present. Add following data to the users.json file and write the index.js file code:
JSON
[
{
"name": "John",
"age": 21,
"language": ["JavaScript", "PHP", "Python"]
},
{
"name": "Smith",
"age": 25,
"language": ["PHP", "Go", "JavaScript"]
}
]
JavaScript
// Requiring users file
const users = require("./users");
console.log(users);
To run the file using the command:
node index.js
Output:

Method 2: Using the fs module:
Another approach to read a file in Node JS is by utilizing the fs module. The fs module provides the file content as a string, requiring us to convert it into JSON format using the built-in method JSON.parse().
JavaScript
const fs = require("fs");
// Read users.json file
fs.readFile("users.json", function(err, data) {
// Check for errors
if (err) throw err;
// Converting to JSON
const users = JSON.parse(data);
console.log(users); // Print users
});
Output:

Writing to a JSON file
We can write data into a JSON file by using the nodejs fs module. We can use writeFile method to write data into a file.
Syntax:
fs.writeFile("filename", data, callback);
Example: We will add a new user to the existing JSON file, we have created in the previous example. This task will be completed in three steps:
- Read the file using one of the above methods.
- Add the data using
.push()
method. - Write the new data to the file using
JSON.stringify()
method to convert data into string.
JavaScript
const fs = require("fs");
// STEP 1: Reading JSON file
const users = require("./users");
// Defining new user
let user =
{
name: "New User",
age: 30,
language: ["PHP", "Go", "JavaScript"]
};
// STEP 2: Adding new data to users object
users.push(user);
// STEP 3: Writing to a file
fs.writeFile(
"users.json",
JSON.stringify(users),
err => {
// Checking for errors
if (err) throw err;
// Success
console.log("Done writing");
});
Output: Run the file again and you will see a message into the console:

Now check your users.json file it will looks something like below:
Similar Reads
How to read and write files in Node JS ? NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada
2 min read
How to Read and Write Excel file in Node.js ? Read and write excel file in Node is a common task while updating the data from backend. There are many packages available on npm for performing these operations.Approach To read and write Excel file in Node js we will use the xlsx package from NPM. We can read the excel file using readFile method a
4 min read
How to read/write JSON File? Ruby is a dynamic, object-oriented programming language developed by Yukihiro Matsumoto in the mid-1990s. Its key features include a simple and elegant syntax, dynamic typing, object-oriented nature, support for mixins and modules, blocks and closures, metaprogramming, and a vibrant community with a
6 min read
How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package.
3 min read
How to work with Node.js and JSON file ? Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. With node.js as backend, the developer can maintain a single codebase for an entire application in javaScript.JSON on the other hand, stands for JavaScript Object Notation. It is a lightweight format for storing and exchanging d
4 min read
How to Download a File Using Node.js? Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.Using
3 min read
How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
4 min read
How To Read File From Amazon S3 Bucket Using Node.Js ? The AWS Simple Storage Service (S3) is a cloud service provided by Amazon Web Services (AWS) to store your data securely. There are different approaches to storing and retrieving data from AWS S3; one of them is by using aws-sdk provided by Amazon Web Services. In this article, we will provide you w
5 min read
How to Update Data in JSON File using Node? To update data in JSON file using Node.js, we can use the require module to directly import the JSON file. Updating data in a JSON file involves reading the file and then making the necessary changes to the JSON object and writing the updated data back to the file. Table of ContentUsing require() Me
4 min read
How to Parse JSON using Node.js? JSON stands for JavaScript Object Notation. The text-based data exchange format data exchange format allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. JSON parsing is a common task when working with data from APIs, configu
2 min read