How do I read a JSON file in node JS

In this post we have learn how do i read a json file in node js. Also we have learn how do i write a json file in node js, node js write json to file, How do I import a JSON file into node js, How do I return a JSON object in node JS.

To include the File System module, use the require() method:

var fs = require('fs');

Example : Read and Write json file in node js

index.js

const fs = require("fs")

const jsondata = {
  name : "Web-tuts.com",
  age : 21
}

const json = JSON.stringify(jsondata);
fs.writeFile("json1.json", json, (err) => {
  console.log("Done");
}) 

fs.readFile("json1.json","utf-8",(err, data) =>{
  const jsonobj = JSON.parse(data);
  console.log(jsonobj.name);
})