Answers for "node js save data to json file"

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

write json file in node js

function writeJsonFile(file, content) {
  let jsonData = JSON.stringify(content)
  fs.writeFileSync(file, jsonData)
}
Posted by: Guest on June-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language