how to load json file in javascript
const fs = require('fs');
fs.readFile('./customer.json', 'utf8', (err, jsonString) => {
if (err) {
console.log("File read failed:", err)
return
}
console.log('File data:', jsonString)
})
3.75
8
fs.readFile(filePath, function (error, content) {
var data = JSON.parse(content);
console.log(data.collection.length);
});
Thank you!
8
0
4.25
4
const fs = require('fs');
let rawdata = fs.readFileSync('student.json');
let student = JSON.parse(rawdata);
console.log(student);
Thank you!
4
0
3.22
9
// in example.json
{
"name": "testing"
}
// in app.js :
// method 1
const data = require('./example.json');
// method 2: (ES6/ES2015)
import * as data from './example.json';
const { name } = data;
console.log(name); // output 'testing'
Thank you!
9
0
Are there any code examples left?
New code examples in category Javascript