header banner
Default

Ways to Read and Write JSON Files in Node Js


It's possible to read and write JSON files in Node.js. All you need to do is utilize the fs Module as detailed in our easy-to-follow guide.

Reading and Writing JSON files in Node.js

The JavaScript Object Notation format, popularly known as JSON, is a lightweight data-transfer format widely used for representing structured data. It is a text-based format that is easy for humans to read and write and for machines to parse and generate.

The ability to programmatically read and write JSON files in Node.js allows you to store, exchange, and manipulate structured data efficiently and easily. Learn how to read, write, and update JSON files using the Node.js file system module.

The Node.js File System Module

The Node.js file system (fs) module is built into Node.js. It lets you interact with the file system on your device. You can use it to read the contents of a file, create a new file, and delete a file, among other things.

The methods provided by the fs module can either be synchronous or asynchronous. Synchronous methods block the execution of your program until the file system operation is complete. These methods usually have "Sync" at the end of their names. For example, readFileSync or writeFileSync.

On the other hand, asynchronous methods do not block the execution of your program and allow it to continue processing other tasks while the file system operation is being performed. These methods accept a callback function that will run when the operation is complete. For example, readFile or writeFile.

When interacting with the file system, you should always use asynchronous methods to maintain the non-blocking nature of the event loop and improve your application’s performance and responsiveness.

However, synchronous methods have their place in certain scenarios, especially when you’re writing simple scripts or dealing with one-time file operations.

Reading JSON Files With the fs Module

To read a JSON file, first import the asynchronous fs module into your main file. Like so:

 const fs = require("node:fs/promises");

If you are using a version lower than Node.js v18, import the fs module like this:

 const fs = require("fs/promises");

If you want to import the entire module (Synchronous and Asynchronous), remove the /promises.

You can read a JSON file using the readFile method which takes two arguments: a file path and an optional configuration object. The config argument specifies options for reading the file and can be an object with options or a string encoding.

The object options include:

  • encoding (string, default is "utf8"): This option specifies the character encoding to use when reading the file. Common encodings include "utf8" for text files and "binary" for binary files.
  • flag (string, default is "r"): This option specifies the file system flag used when opening the file. Common flags include "r" for reading and "w" for writing.

For example:

 fs.readFile("./users.json", { encoding: "utf-8", flag: "r" })
  .then((data) => {
    const users = JSON.parse(data);
    console.log(users);
  })
  .catch((error) => {
    console.error('Error reading the JSON file:', error);
  });

This code reads a JSON file called users.json in the current directory. When you retrieve the file’s data, you can parse it from JSON into a JavaScript object using JSON.parse. This allows you to access and manipulate the data as an object in your code.

For small JSON files, you can use require to read them synchronously. This method automatically parses JSON files into JavaScript objects. For larger JSON files and in non-blocking scenarios, use fs.readFile to read them asynchronously. Additionally, using require also caches the file contents in memory, so it might not be ideal if your JSON file changes a lot.

Writing JSON Files With the fs Module

You can write data to JSON files using the writeFile method. This method takes three arguments:

  • A file path.
  • The data you want to write to the file, which can be a string, a buffer, an AsyncIterable, or an Iterable object.
  • An optional configuration object.

This method asynchronously writes data to a file. If the file exists, it overwrites the existing content with the new content. If the file doesn't exist, it creates it and populates it with the data you pass as an argument.

For example:

 const fakeUsers = [
  {
    id: 1,
    name: "John Doe",
    username: "johndoe123",
    address: {
      street: "123 Main St",
      city: "Anytown",
    },
  },
  {
    id: 2,
    name: "Jane Smith",
    username: "janesmith456",
    address: {
      street: "456 Elm St",
      city: "Another City",
    },
  }
];

fs.writeFile("./users.json", JSON.stringify(fakeUsers), {
  encoding: "utf-8",
  flag: "w",
}).catch((error) => {
  console.error('Error writing the JSON file:', error);
});

The data you pass into the writeFile function has to be a string or a buffer, so if you want to write an object to the file, you must first convert it into a string using the JSON.stringify method.

Updating JSON Files With the fs Module

The fs module doesn't provide an explicit way to update files, as writing a file overwrites any existing data.

To work around this, you can update a file by first getting the existing content from the file using the readFile method. Then, you can add the existing data to your current data and pass it as your data argument in the writeFile method.

Here’s a function that implements the logic above:

 const updateFile = async (filePath, data) => {
  try {
    const fileContents = await fs.readFile(filePath, {
      encoding: "utf-8",
      flag: "r",
    });

    const fileData = JSON.parse(fileContents);

    const updatedFileData = [...fileData, ...data];

    await fs.writeFile(filePath, JSON.stringify(updatedFileData), {
      encoding: "utf-8",
      flag: "w",
    });

    return "File updated successfully";
  } catch (error) {
    console.error('Error updating the JSON file:', error);
  }
};

You can call the function like so:

 updateFile("./users.json", [
  {
    id: 4,
    name: "Jane Doe",
    username: "janedoe123",
    address: {
      street: "123 Main St",
      city: "Anytown",
    },
  },
  {
    id: 5,
    name: "John Smith",
    username: "johnsmith456",
    address: {
      street: "456 Elm St",
      city: "Another City",
    },
  }
]).then((message) => {
  console.log(message);
});

This code block will add the users with the information above to the existing users.json file.

Security Considerations for Reading and Writing JSON Files

Safeguarding your Node.js application when reading and writing JSON files involves crucial security considerations. You should always validate the JSON data to ensure it conforms to your expectations. You should also restrict file access permissions and sanitize user input to thwart potential vulnerabilities like code injection.

Sources


Article information

Author: David Bird

Last Updated: 1704125282

Views: 1076

Rating: 3.7 / 5 (117 voted)

Reviews: 97% of readers found this page helpful

Author information

Name: David Bird

Birthday: 2014-01-23

Address: 6406 Brittney Way, South Marcia, AR 27260

Phone: +3825816918863722

Job: Biomedical Engineer

Hobby: Cocktail Mixing, Horseback Riding, Camping, Golf, Rowing, Tea Brewing, Painting

Introduction: My name is David Bird, I am a forthright, rare, receptive, talented, unswerving, audacious, spirited person who loves writing and wants to share my knowledge and understanding with you.