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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read