iwchqw
Last Updated: May 29, 2017
·
185
· Codi

Javascript write json to file with nodejs

You can use this write json file Javascript component to write json to file.
Example:

const writeJsonFile = require('bit/global/write-json-file'); const jsonFile = "/tmp/exampleFile.json"; writeJsonFile(jsonFile,{isThisReal:ture,author:amit}).catch(err => console.log(err));

Src code:

`const fs = require('fs');
const vaildateJson = require('bit/global/json-validator');

const writeToFile = (file,data) => {
const jsonStr = (data instanceof Object) ? JSON.stringify(data,null,4) : data ;
return vaildateJson(jsonStr)
.then(jsonStr =>
new Promise((resolve, reject) => {
fs.writeFile(${file}, jsonStr, 'utf-8', function(err) {
if (err) reject(err);
else resolve(jsonStr);
});
}))
}

module.exports = function writeJsonFile(file, data) {
return writeToFile(file,data)
};`

Say Thanks
Respond
Awesome Job

A3e45f3a cade 11e7 83bc 71c9546a2749
Systems Engineer
·
Fulton, Maryland or Remote
·
Full Time