1const fs = require('fs');
2const path = require('path');
3
4let rawdata = fs.readFileSync(path.resolve(__dirname, 'student.json'));
5let student = JSON.parse(rawdata);
6console.log(student);
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
1function readJsonFile(file) {
2 let bufferData = fs.readFileSync(file)
3 let stData = bufferData.toString()
4 let data = JSON.parse(stData)
5 return data
6}
1D:\NodeJs>node readsync.js
2 *START*
3Output Content:
4 /* content here */
5*EXIT*
6// Define JSON File
7 var fs = require("fs");
8 console.log("\n *STARTING* \n");
9// Get content from file
10 var contents = fs.readFileSync("jsoncontent.json");
11// Define to JSON type
12 var jsonContent = JSON.parse(contents);
13// Get Value from JSON
14 console.log("User Name:", jsonContent.username);
15 console.log("Email:", jsonContent.email);
16 console.log("Password:", jsonContent.password);
17log("\n *EXIT* \n");
18
1config = require("./yourfile.json"); // Note, "config" can be changed to your liking.
1// Read Synchrously
2var fs = require("fs");
3console.log("\n *START* \n");
4var content = fs.readFileSync("content.txt");
5console.log("Output Content : \n"+ content);
6console.log("\n *EXIT* \n");
7