How to Read and Write Data to a JSON File Using Node.js

Robin
Updated on April 5, 2023

JSON (JavaScript Object Notation) is one of the most popular formats for storing and exchanging data. Therefore, it is essential to know how to work with JSON data.

If you are building a web application, you can store some data in a JSON file or transfer information from one system to another in this format. You must also know how to read and write a JSON file.

In this post, we will use Node.js to read information from a JSON file and also write data to it. We will go through every possible way to handle a JSON file in Node.js along with their advantages and drawbacks.

How to Read Data From a JSON File in Node.js

There are mainly 2 different ways to access data from a JSON file in a Node.js application. I will show you both of these techniques. Because they have their own use cases. You can apply them based on the requirements of your project.

To read data from a JSON file using Node.js, you can use the built-in require() method to load the file and automatically parse the JSON data into a JavaScript object.

If you want more control, you can use the readFile() method from the fs module to extract data from a JSON file. Because it will return the updated data from your file.


Method 1: Reading JSON Files Using require() Function

The easiest way to read a JSON file and get information out of it is by using the require() function. You can call this function with the path of your JSON file. It will automatically parse your JSON data and return it as an object.

          const data = require('./languages.json')

console.log(data)
        

Output:

          [
    { id: 1, name: 'JavaScript', slug: 'js' },
    { id: 2, name: 'Python', slug: 'python' },
    { id: 3, name: 'Golang', slug: 'golang' },
    { id: 4, name: 'Rust', slug: 'rust' },
]
        

As you can see, the require() function is extracting and parsing data from my languages.json file at the same time. Therefore, I am getting the data as an array of objects.

Using The import Statement

If you are using ES6 modules with Node.js then you can read your JSON files using the import statement as well. This also works like the require() function.

          import data from './languages.json' assert { type: 'json' }

console.log(data)

        

Output:

          [
    { id: 1, name: 'JavaScript', slug: 'js' },
    { id: 2, name: 'Python', slug: 'python' },
    { id: 3, name: 'Golang', slug: 'golang' },
    { id: 4, name: 'Rust', slug: 'rust' },
]
        

Note that you have to use the assertion { type: "json" } with the import statement. This import assertion helps the loader to understand that the imported file is JSON data. So, it will parse it correctly.

Also Read: How to Use ES6 Modules Import and Export Syntax in Node.js

But there is a limitation you need to be aware of while using require() function or the import statement. If you use this technique in your Node.js server, it will only read the JSON file once when the server starts.

If you update your JSON file, the server will serve the cached version. It won't have access to the updated data. Every time you change your file, you also have to restart your server.

Therefore, this technique is useful to read JSON files that don't change often like configuration files. But if you want to update your JSON file and get the latest data, you can follow the next technique.


Method 2: Reading JSON Files Using fs Module

To read a JSON file in real time, you can make use of the Node.js fs module. It has a readFile() method that extracts data from a file.

This module has 2 methods to read files both synchronously and asynchronously. The readFile() method reads a file asynchronously and the readFileSync() method does it synchronously.

It is always recommended to use asynchronous methods as much as possible. Because these don't block the main thread. So it improves the performance of your application.

If you don't know the difference between synchronous and asynchronous methods, you can read the following post on this topic.

Also Read: Synchronous VS Asynchronous Programming with Examples

Reading JSON Files Asynchronously Using readFile() Method

This method accepts 2 arguments. The first argument is a path of a file (in this case a JSON file) and the second argument is an options object (it's optional).

          const { readFile } = require('fs/promises')

const getData = async () => {
    try {
        // Getting JSON string
        const res = await readFile('./languages.json', { encoding: 'utf8' })

        // Parsing JSON string
        const data = JSON.parse(res)

        console.log(data)
    } catch (err) {
        // Handle errors here
        console.log(err)
    }
}

getData()
        

Output:

          [
    { id: 1, name: 'JavaScript', slug: 'js' },
    { id: 2, name: 'Python', slug: 'python' },
    { id: 3, name: 'Golang', slug: 'golang' },
    { id: 4, name: 'Rust', slug: 'rust' },
]
        

In this example, I am using the promised version of the readFile() method. You can use the callback version if you want. Both work in the same way.

Here I am passing the path of my languages.json file and setting the encoding option as "utf8" for this method. If you don't set this option, it will return the data as a buffer.

Now you will get the response from this method as a JSON string. To use the data in your application as a JavaScript object, you have to parse using the JSON.parse() method.

Reading JSON Files Synchronously Using readFileSync() Method

If you want to read the JSON file synchronously in your application for any reason, you can do it using the readFileSync() method from the fs module.

          const { readFileSync } = require('fs')

const getDataSync = () => {
    try {
        // Getting JSON string
        const res = readFileSync('./languages.json', { encoding: 'utf8' })

        // Parsing JSON string
        const data = JSON.parse(res)

        console.log(data)
    } catch (err) {
        // Handle errors here
        console.log(err)
    }
}

getDataSync()
        

Output:

          [
    { id: 1, name: 'JavaScript', slug: 'js' },
    { id: 2, name: 'Python', slug: 'python' },
    { id: 3, name: 'Golang', slug: 'golang' },
    { id: 4, name: 'Rust', slug: 'rust' },
]
        

You have to follow the same steps. The readFileSync() method also accepts 2 arguments: a file path and an options object. As this method works synchronously, you can't use the async/await syntax.

Also Read: How to Get Any File Size in Node.js: Mastering the fs Module


How to Write Data to a JSON File in Node.js

To write data to a JSON file, there are 2 methods in the Node.js fs module. The writeFile() method adds data to a file asynchronously and the writeFileSync() method to it synchronously.

Whichever method you use, you have to follow the same process. First, convert your JavaScript object into a JSON string using JSON.stringify() method and then pass this string to the writeFile() or writeFileSync() method.


Method 1: Write to JSON Files Using fs.writeFile Method

This method accepts 2 arguments. The first argument is the path of a file and the second argument is the data you want to write to that file.

          const { writeFile } = require('fs/promises')

const writeData = async () => {
    try {
        // New data
        const newLang = {
            id: 5,
            name: 'Python',
            slug: 'python',
        }

        // Stringify data
        const jsonString = JSON.stringify(newLang)

        // Writing data
        await writeFile('./languages.json', jsonString)
    } catch (err) {
        // Handle errors here
        console.log(err)
    }
}

writeData()
        

Output:

          {"id":5,"name":"Python","slug":"python"}
        

In this example, I am using the promised version of the writeFile() method so that I can make use of the async/await syntax instead of callbacks.

This technique will override the previous data of that file. If the langages.json file has any data, it will delete those and add the new one. In the next section, I will show you how to update or append new data to the existing data.

By default, the JSON.stringify() method converts the string into a single line (minified version). But if you want to add spaces in your JSON string, you can do it as well.

          const jsonString = JSON.stringify(newLang, null, 2)
        

Output:

          {
  "id": 5,
  "name": "Python",
  "slug": "python"
}
        

Instead of just passing the JavaScript object to this method, also pass 2 other arguments. Set null for the second argument and "2" for the third argument.

This number represents how many spaces you want to add in front of each line. If you need more space, you will increase this number.

Also Read: How to Read/Parse CSV Files in Node.js with Examples


Method 2: Write to JSON Files Using fs.writeFileSync Method

You can use the writefileSync() method from the fs module to write your JSON file synchronously. The process is the same. Stringify your object and then pass it to this method.

          const { writeFileSync } = require('fs')

const writeDataSync = () => {
    try {
        // New data
        const newLang = {
            id: 5,
            name: 'Python',
            slug: 'python',
        }

        // Stringify data
        const jsonString = JSON.stringify(newLang, null, 2)

        // Writing data
        writeFileSync('./languages.json', jsonString)
    } catch (err) {
        // Handle errors here
        console.log(err)
    }
}

writeDataSync()
        

Output:

          {
  "id": 5,
  "name": "Python",
  "slug": "python"
}
        

Just replace the writeFile() method with the writeFileSync() method and remove the async/await syntax because it is a synchronous method.

In this example, I am creating my JSON string with 2 spaces using JSON.stringify() method. But as you can see, it is also removing the previous data from my languages.json file. Let's learn how to add new data while keeping existing ones.

Also Read: How to Check If a Directory Exists Using Node.js (Examples)


How to Update/Append Data to a JSON File

If you want to update a JSON file or add new data to it, you need to follow these 3 steps:

  • Step 1: Read the existing data from the JSON file.
  • Step 2: Add new data to the existing ones.
  • Step 3: Write the updated data in the JSON file.

Suppose in your languages.json file, you have an array with these 4 items:

          [
  {
    "id": 1,
    "name": "JavaScript",
    "slug": "js"
  },
  {
    "id": 2,
    "name": "TypeScript",
    "slug": "ts"
  },
  {
    "id": 3,
    "name": "Golang",
    "slug": "golang"
  },
  {
    "id": 4,
    "name": "Rust",
    "slug": "rust"
  }
]
        

Now you want to add the 5th item to this array and save it in your JSON file. This is how you can achieve this.

          const { readFile, writeFile } = require('fs/promises')

const updateFile = async () => {
    try {
        // Getting existing data
        const data = await readFile('./languages.json', { encoding: 'utf8' })

        // Parsing data
        const languages = JSON.parse(data)

        // New object
        const newLang = {
            id: 5,
            name: 'Python',
            slug: 'python',
        }

        // Creating new array with previous objects and new object
        const newArray = [...languages, newLang]

        // Stringify thw new array
        const jsonString = JSON.stringify(newArray, null, 2)

        // Writing to the file
        await writeFile('./languages.json', jsonString)
    } catch (err) {
        // Handle error here
        console.log(err)
    }
}

updateFile()
        

Output:

          [
  {
    "id": 1,
    "name": "JavaScript",
    "slug": "js"
  },
  {
    "id": 2,
    "name": "TypeScript",
    "slug": "ts"
  },
  {
    "id": 3,
    "name": "Golang",
    "slug": "golang"
  },
  {
    "id": 4,
    "name": "Rust",
    "slug": "rust"
  },
  {
    "id": 5,
    "name": "Python",
    "slug": "python"
  }
]
        

Step 1: I am getting the existing array from the languages.json file using readFile() method. Then parsing it to a JavaScript array of objects.

Step 2: I am creating a new array with the previous objects and the new object that I want to add.

Step 3: Finally, I am converting the new array into a JSON string and write that string to the languages.json file again.

By following these 3 steps, you can easily append any new data to your JSON files. To update an existing object, the process is also very similar.

First, you need to find the object from your array of objects and update it. Then create a new array with the remaining objects and the updated object. Finally, write this new array to your JSON file.


Conclusion

As you have learned, there are several ways to read and write JSON files in a Node.js application. If your file doesn't change very often, you can read this file using require() function or the import statement.

But if your JSON file changes frequently and you need to access the updated data in real-time then the readFile() or readFileSync() method can be used from the fs module.

On the other hand, for writing data to a JSON file, the fs module has writeFile() and writeFileSync() methods. You can use any one of these.

It is also possible to update a file or append new data to the existing JSON file by utilizing both reading and writing techniques. By understanding different options, you can choose the best approach required for your application.

Related Posts