how to use the writejson function from fs-extra in javascript

To use the writeJson function from fs-extra in Javascript, you need to first install the module using npm.

index.tsx
npm install fs-extra
21 chars
2 lines

Then, you can require the module and use the writeJson function to write the JSON data to a file.

index.tsx
const fs = require('fs-extra');

// Sample JSON object
const data = {
  name: 'John Doe',
  age: 30,
  city: 'New York'
};

// Writing JSON data to file
fs.writeJson('data.json', data)
  .then(() => {
    console.log('Write successful');
  })
  .catch(err => {
    console.error(err);
  });
291 chars
18 lines

In the above example, we have created a sample JSON object data and then passed it to the writeJson function along with the filename (data.json) to which we want to save the data. The function returns a Promise that resolves when the write operation is successful.

gistlibby LogSnag