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

How to read JSON file using Node.js

不言
Release: 2019-03-26 14:03:40
Original
7407 people have browsed it

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
Copy after login

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"
  }
]
Copy after login

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("----------------------------------");
  }
});
Copy after login

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
----------------------------------
Copy after login

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("----------------------------------");
}
Copy after login

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
----------------------------------
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template