read json data from json file in javascript code example

Example 1: javascript parse json

var jsonPerson = '{"first_name":"billy", "age":23}';
var personObject = JSON.parse(jsonPerson); //parse json string into JS object

Example 2: read json file into object javascript

fs.readFile(filePath, function (error, content) {
    var data = JSON.parse(content);
    console.log(data.collection.length);
});

Example 3: read json from file js

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)
})