How to use require() Method In Javascript

We are going to use a Direct Approach, In this approach we can directly import the json data file and update like we normally update the JavaScript object. In last to update the changes in a Json file that we have made, we need to use a writeFileSync() function of a File System Module to update the changes.

Syntax:

const fs = require('fs');

// Read the JSON file
const jsonData = require('./data.json');

// Update the data
jsonData.key = 'new value';

// Write the updated data back to the JSON file
fs.writeFileSync('./data.json', JSON.stringify(jsonData, null, 2));

console.log('Data updated successfully.');

Before Updating data.json file:

{
"key": "old value"
}

After Updating data.json file:

{
"key": "new value"
}

Steps to create Node.js Application:

Step 1: Create a a folder for your application and move to that folder using below command.

cd foldername

Step 2: Create a NodeJS application using the following command:

npm init

Example : The below example demonstrate updating data in json file using require() method

JavaScript
//File path: /index.js
const fs = require('fs');

// Read the JSON file
const jsonDataBefore = require('./data.json');

// Print JSON data before updating
console.log('Before updating JSON:');
console.log(JSON.stringify(jsonDataBefore, null, 2));

// Update the data
jsonDataBefore[0].programmingLanguage.push("JavaScript");
jsonDataBefore[1].programmingLanguage.push("JavaScript");

// Write the updated data back to the JSON file
fs.writeFileSync('./data.json', JSON.stringify(jsonDataBefore, null, 2));

// Print JSON data after updating
console.log('\nAfter updating JSON:');
console.log(JSON.stringify(jsonDataBefore));

console.log('\nData updated successfully.');
JavaScript
//File path: data.json (note: remove this comment line)

[
    { "name": "John", "programmingLanguage": ["Python"] },
    { "name": "Alice", "programmingLanguage": ["C++"] }
]

To run the application use the following command:

node index.js 

Output:

Before updating JSON:
[
{
"name": "John",
"programmingLanguage": [
"Python"
]
},
{
"name": "Alice",
"programmingLanguage": [
"C++"
]
}
]

After updating JSON:
[
{
"name": "John",
"programmingLanguage": [
"Python",
"JavaScript"
]
},
{
"name": "Alice",
"programmingLanguage": [
"C++",
"JavaScript"
]
}
]

Data updated successfully.

How to Update Data in JSON File using JavaScript ?

JSON stands for JavaScript Object Notation. It is a text-based data exchange format that allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. Updating data in a JSON file involves reading the file and then making the necessary changes to the JSON object and then writing the updated data back to the file.

Table of Content

  • Using require() Method
  • Using File System Module

Similar Reads

Using require() Method

We are going to use a Direct Approach, In this approach we can directly import the json data file and update like we normally update the JavaScript object. In last to update the changes in a Json file that we have made, we need to use a writeFileSync() function of a File System Module to update the changes....

Using File System Module

We are going to use a File System Module (“fs”). File System Module provides a different different functions to read and write the data to the file. We can use fs.readFileSync(file_name) or fs.readFile(file_name) function to read the content of the data.json file....