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 Get/Fetch/Read Data from Json File in Node JS?
Node.js comes with built-in support for handling JSON data, making it simple to read and write JSON files. Here is a step-by-step guide on how to get/fetch data from a JSON file in Node.js.
- Step 1: Create a JSON file
- Step 2: Load the JSON file
- Step 3: Parse the JSON data
- Step 4: Access the JSON data
Step 1: Create a JSON file
The first step is to create a JSON file containing the data you want to fetch. You can create the file using any text editor, and save it with a .json extension. Here is an example of a simple JSON file:
{ "name": "John Doe", "age": 30, "email": "[email protected]" }
Step 2: Load the JSON file
To load the JSON file into your Node.js application, you can use the built-in fs
module, which provides an API for interacting with the file system. The fs
module includes a method called readFileSync
, which reads the contents of a file synchronously and returns it as a string.
Here is an example code snippet that loads the JSON file:
const fs = require('fs'); const data = fs.readFileSync('data.json', 'utf8');
In this code, you first import the fs
module and then call the readFileSync
method to read the contents of the data.json
file. The second argument to readFileSync
is the encoding of the file, which set to utf8
to ensure that the data is returned as a string.
Step 3: Parse the JSON data
The data returned by readFileSync
is a string, but you need to parse it into a JavaScript object so that can work with it in our code. To parse the JSON data, you can use the built-in JSON.parse
method, which takes a JSON string as input and returns a JavaScript object.
Here is an example code snippet that parses the JSON data:
const jsonData = JSON.parse(data);
In this code, you call the JSON.parse
method and pass in the data
string that loaded from the file. The result is a JavaScript object that you can now work with in our code.
Step 4: Access the JSON data
Now that you have loaded and parsed the JSON data, and can access it just like any other JavaScript object. Here is an example code snippet that accesses the name
property of the JSON data:
console.log(jsonData.name);
This code logs the value of the name
property 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.