How to Get/Fetch Data from Json File in Node JS

1. Use the Node.js ‘fs’ module to read the JSON file:

const fs = require(‘fs’);

// Get content from file
let jsonData = fs.readFileSync(‘data.json’);

// Parse JSON string into object
let data = JSON.parse(jsonData);

2. Use the Node.js ‘request’ module to fetch the JSON file:

const request = require(‘request’);

request(‘http://example.com/data.json’, (error, response, body) => {
if (!error && response.statusCode == 200) {
let data = JSON.parse(body);
}
});

Get data from json file in node js; Through this tutorial, you will learn how to fetch/read/get data from json file in Node js apps.

Node.js is a popular runtime environment for JavaScript that allows developers to write server-side applications using JavaScript. One of the common tasks in a Node.js application is to read data from a JSON file. In this article, you will learn how to get data from a JSON file in Node.js, including the syntax and some examples.

How to Fetch/Read Data from Json File in Node JS?

To get data from a JSON file in Node.js, including the syntax and some examples:

  • Syntax for Reading JSON Data in Node.js
  • Example of Getting Data from a JSON File in Node.js

Syntax for Reading JSON Data in Node.js

Node.js provides a built-in module called “fs” (short for “file system”) that allows developers to read data from files. To read data from a JSON file, you can use the following syntax:

const fs = require('fs');
const data = fs.readFileSync('file.json');
const jsonData = JSON.parse(data);

In this code snippet, you first require the “fs” module, which provides us with functions for working with the file system. Then, you use the “readFileSync” function to read the contents of the file “file.json” synchronously (i.e., the code will block until the file has been read). Finally, you use the “JSON.parse” function to parse the JSON data and convert it into a JavaScript object.

Example of Getting Data from a JSON File in Node.js

Let’s say you have a JSON file named “data.json” with the following contents:

{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}

To read this data in a Node.js application, you can create a file called “app.js” with the following code:

const fs = require('fs');
const data = fs.readFileSync('data.json');
const jsonData = JSON.parse(data);

console.log(jsonData.name); // Output: John Doe
console.log(jsonData.age); // Output: 30
console.log(jsonData.city); // Output: New York

In this code snippet, You first require the “fs” module and use the “readFileSync” function to read the contents of the “data.json” file. Then, you use the “JSON.parse” function to parse the JSON data and store it in the “jsonData” variable. Finally, you log the values of the “name”, “age”, and “city” properties of the JSON data to the console.

Conclusion

Reading data from a JSON file is a common task in Node.js applications. By using the built-in “fs” module and the “JSON.parse” function, you can easily read and parse JSON data in Node.js. It’s important to ensure that the JSON data is well-formed and valid to avoid errors when parsing the data. With these techniques in mind, developers can leverage the power of JSON data in their Node.js applications.

Recommended Node JS Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?