Friday, 19 October 2018

Node.js: Load json file using require function

You can use ‘require’ function to load the json file data.

For example,

employee.json
{
 "org": [{
  "name": "Honeywell",
  "yrsOfExperience": 2.2
 }, {
  "name": "IBM",
  "yrsOfExperience": 1.8
 }],
 "firstName": "Krishna",
 "lastName": "Hari",
 "salary": 28000.0
}

HelloWorld.json
var data = require('./employee.json');

console.log(data);

Run HelloWorld.json, you can see below messages in console.

{ org:
   [ { name: 'Honeywell', yrsOfExperience: 2.2 },
     { name: 'IBM', yrsOfExperience: 1.8 } ],
  firstName: 'Krishna',
  lastName: 'Hari',

  salary: 28000 }


Previous                                                 Next                                                 Home

No comments:

Post a Comment