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);
1// in example.json
2{
3 "name": "testing"
4}
5
6// in app.js :
7
8// method 1
9const data = require('./example.json');
10
11// method 2: (ES6/ES2015)
12import * as data from './example.json';
13
14const { name } = data;
15console.log(name); // output 'testing'
1const fs = require('fs');
2
3fs.readFile('./customer.json', 'utf8', (err, jsonString) => {
4 if (err) {
5 console.log("File read failed:", err)
6 return
7 }
8 console.log('File data:', jsonString)
9})
1const fs = require('fs');
2
3let rawdata = fs.readFileSync('student.json');
4let student = JSON.parse(rawdata);
5console.log(student);
6
1fs.readFile(filePath, function (error, content) {
2 var data = JSON.parse(content);
3 console.log(data.collection.length);
4});
5