Home  >  Article  >  Web Front-end  >  How to read JSON file using Node.js

How to read JSON file using Node.js

不言
不言Original
2019-03-26 14:03:407377browse

This article will introduce about using node.js to read the content of JSON files, using the readfile and readfilesync functions of the JSonfile module.

How to read JSON file using Node.js

#Requirements: node.js and npm must be installed on the system.

For this article, we are using the JSonfile NPM module. Therefore, first you need to install the JSonfile module on your system

$ npm install jsonfile --save

Now, a virtual json file employee.json is being created. It is also possible to use your own JSON file.

File name: employee.json

[
  {
    "emp_id" : "101",
    "emp_name" : "Mike",
    "emp_addr" : "123 California, USA",
    "designation" : "Editor"
  },
  {
    "emp_id" : "102",
    "emp_name" : "Jacob",
    "emp_addr" : "456 Log Angelis, USA",
    "designation" : "Chief Editor"
  }
]

1. Use nodejs to read the json file

In the above steps, a JSON file was created Example. Now create ReadJsonFile.js and add the following content. Need to change employee.json with JSON file name.

File name: ReadJsonFile.js

var jsonFile = require('jsonfile')
var fileName = 'employee.json'

jsonFile.readFile(fileName, function(err, jsonData) {
  if (err) throw err;
  for (var i = 0; i < jsonData.length; ++i) {

    console.log("Emp ID: "+jsonData[i].emp_id);
    console.log("Emp Name: "+jsonData[i].emp_name);
    console.log("Emp Address: "+jsonData[i].emp_addr);
    console.log("Designation: "+jsonData[i].designation);
    console.log("----------------------------------");
  }
});

Now run the nodejs script using the following command.

$ node ReadJsonFile.js 

Emp ID: 101
Emp Name: Mike
Emp Address: 123 California, USA
Designation: Editor
----------------------------------
Emp ID: 102
Emp Name: Jacob
Emp Address: 456 Log Angelis, USA
Designation: Chief Editor
----------------------------------

2. Use nodejs to read the json file

Alternatively, you can use the readfilesync function to read the JSON file content. Create a readjsonfilesync.js file with the following content

File name: readjsonfilesync.js

var jsonFile = require(&#39;jsonfile&#39;)
var fileName = &#39;employee.json&#39;

var jsonData = jsonFile.readFileSync(fileName);

for (var i = 0; i < jsonData.length; ++i) {

	console.log("Emp ID : "+jsonData[i].emp_id);
	console.log("Emp Name : "+jsonData[i].emp_name);
	console.log("Emp Address : "+jsonData[i].emp_addr);
	console.log("Designation : "+jsonData[i].designation);
	console.log("----------------------------------");
}

Now run the nodejs script using the following command.

$ node ReadJsonFileSync.js 

Emp ID: 101
Emp Name: Mike
Emp Address: 123 California, USA
Designation: Editor
----------------------------------
Emp ID: 102
Emp Name: Jacob
Emp Address: 456 Log Angelis, USA
Designation: Chief Editor
----------------------------------

This article has ended here. For more other exciting content, you can pay attention to the node.js video tutorial column on the PHP Chinese website! ! !

The above is the detailed content of How to read JSON file using Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn