Answers for "how to write to a json file in javascript"

9

HOW WRITE AND SAVE JSON FILE IN NODEJS

'use strict';

const fs = require('fs');

let student = { 
    name: 'Mike',
    age: 23, 
    gender: 'Male',
    department: 'English',
    car: 'Honda' 
};
 
let data = JSON.stringify(student, null, 2);

fs.writeFile('student-3.json', data, (err) => {
    if (err) throw err;
    console.log('Data written to file');
});

console.log('This is after the write call');
Posted by: Guest on September-30-2020
1

js write to json file

fs.writeFile ("file.json", JSON.stringify(data), function(err) {
    if (err) throw err;
    console.log('complete');
    }
);
Posted by: Guest on March-12-2021
6

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');
    }
);
Posted by: Guest on March-23-2020
0

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')
  }
});
Posted by: Guest on February-14-2021
9

write json file nodejs

const fs = require('fs');
const path = require('path');

let student = { 
    name: 'Mike',
    age: 23, 
    gender: 'Male',
    department: 'English',
    car: 'Honda' 
};
 
fs.writeFileSync(path.resolve(__dirname, 'student.json'), JSON.stringify(student));
Posted by: Guest on April-13-2020

Code answers related to "how to write to a json file in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language