writeJson() function in fs-extra - NodeJS


writeJson() writes an object to a JSON file by parsing.

Syntax

writeJson(file, object[, options] [, callback])

Parameters

  • file – String parameter which will contain name and location of the JSON file.

  • object – Object passed into the writeJson function.

  • options – The 'outputFile' function supports the following options −

    • spaces – The number of spaces will be passed in this parameter for indentation.

    • EOL – Setting the 'end of line' character, Default is '
      '.

    • replacer – It takes two parameters – key and value. Will replace If key found, the value will be replaced by the given value.

  • callback – This function will give a callback if any error occurs.

Example 1

  • Check that fs-extra is installed before proceeding; if not, install fs-exra.

  • You can use the following command to check whether fs-extra is installed or not.

npm ls fs-extra
  • Create a writeJsonAsyncExample.js and copy-paste the following code snippet into that file.

  • Now, run the following command to run the following code snippet.

node writeJsonAsyncExample.js

Code Snippet

const fs = require('fs-extra')

// Writing JSON with a callback:
fs.writeJson('./package.json', {name: 'fs-extra'}, err => {
   if (err) return console.error(err)
   console.log('JSON written successfully with callbacks!')
})

// Writing JSON with Promises:
fs.writeJson('./package.json', {name: 'fs-extra'})
.then(() => {
   console.log('JSON written successfully with Promises!')
})
.catch(err => {
   console.error(err)
})

// Writing JSON with async/await:
async function writeJsonAsyncExample () {
   try {
      await fs.writeJson('./package.json', {name: 'fs-extra'})
      console.log('JSON written successfully with Await!')
   } catch (err) {
      console.error(err)
   }
}

writeJsonAsyncExample()

Output

C:\Users\tutorialsPoint\> node writeJsonAsyncExample.js
JSON written successfully with Promises!
JSON written successfully with Await!
JSON written successfully with callbacks!

Introduction to writeJsonSync()

This method writes objects to a JSON file.

Syntax

writeJsonSync(file, object[, options])

Parameters

  • file – String parameter which will contain name and location of the JSON file.

  • object – Object passed into the writeJson function.

  • options – The 'outputFile' function supports the following options −

  • spaces – The number of spaces will be passed in this parameter for indentation.

  • EOL – Setting the 'end of line' character, Default is '
    '.

  • replacer – It takes two parameters – key and value. Will replace If key found, the value will be replaced by the given value.

Example

  • Check that fs-extra is installed before proceeding; if not, install fs-exra.

  • You can use the following command to check whether fs-extra is installed or not.

npm ls fs-extra
  • Create a writeJsonSyncExample.js and copy-paste the following code snippet into that file.

  • Now, run the following command to run the following code snippet.

node writeJsonSyncExample.js

Code Snippet

const fs = require('fs-extra')

fs.writeJsonSync('./package.json', {name: 'fs-extra'})
console.log('Successfully written to JSON !')

Output

C:\Users\tutorialsPoint\> node writeJsonSyncExample.js
Successfully written to JSON !

Updated on: 28-Apr-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements