how to write in json file using 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: how to write to a json file in javascript

const fs = require('fs');
const data = {
  data: "abc",
  stuff: 10,
  more_data: "1 2 3 4 3 2 1",
};
const jsonString = JSON.stringify(data);
fs.writeFile('./data.json', jsonString, err => {
  if (err) {
    console.log('Error writing file', err)
  } else {
    console.log('Successfully wrote file')
  }
});