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)
};`
Written by Codi
Related protips
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside