1'use strict';
2
3const fs = require('fs');
4
5let student = {
6 name: 'Mike',
7 age: 23,
8 gender: 'Male',
9 department: 'English',
10 car: 'Honda'
11};
12
13let data = JSON.stringify(student, null, 2);
14
15fs.writeFile('student-3.json', data, (err) => {
16 if (err) throw err;
17 console.log('Data written to file');
18});
19
20console.log('This is after the write call');
21
1fs.writeFile ("file.json", JSON.stringify(data), function(err) {
2 if (err) throw err;
3 console.log('complete');
4 }
5);
1var fs = require('fs');
2
3var data = {}
4data.table = []
5for (i=0; i <26 ; i++){
6 var obj = {
7 id: i,
8 square: i * i
9 }
10 data.table.push(obj)
11}
12fs.writeFile ("input.json", JSON.stringify(data), function(err) {
13 if (err) throw err;
14 console.log('complete');
15 }
16);
1const fs = require('fs');
2const data = {
3 data: "abc",
4 stuff: 10,
5 more_data: "1 2 3 4 3 2 1",
6};
7const jsonString = JSON.stringify(data);
8fs.writeFile('./data.json', jsonString, err => {
9 if (err) {
10 console.log('Error writing file', err)
11 } else {
12 console.log('Successfully wrote file')
13 }
14});
1const fs = require('fs');
2const path = require('path');
3
4let student = {
5 name: 'Mike',
6 age: 23,
7 gender: 'Male',
8 department: 'English',
9 car: 'Honda'
10};
11
12fs.writeFileSync(path.resolve(__dirname, 'student.json'), JSON.stringify(student));
13