write json object to file in javascript code example
Example 1: js writing to json file
var fs = require('fs');
var data = {}
data.table = []
for (i=0; i <26 ; i++){
var obj = {
id: i,
square: i * i
}
data.table.push(obj)
}
fs.writeFile ("input.json", JSON.stringify(data), function(err) {
if (err) throw err;
console.log('complete');
}
);
Example 2: js write to json file
fs.writeFile ("file.json", JSON.stringify(data), function(err) {
if (err) throw err;
console.log('complete');
}
);