NodeJS provides numerous packages and methods to work with JSON data. But sometimes you may require something as simple as parsing JSON data on your server. In this article, we will look at how to parse JSON in NodeJS.
How to Parse JSON in NodeJS
Here are the different ways to parse JSON in NodeJS.
1. Parse JSON String
If you have a JSON string in your NodeJS server, you can easily parse it using JSON.parse() function. JSON is an in-built library and you do not need to import it into your NodeJS app/server.
Here is a simple example.
var data = '{"name":"Joe","age":"30"}' var users = JSON.parse(string_data);
You can place this code inside request handlers to parse JSON data received in requests sent via client browsers.
2. JSON in File
If your data is stored in a .json file, you need to import it into your NodeJS server using require statement before you can parse it. Here is an example to parse file data.json. You need to provide full path in require statement, else it look will for the file in the same directory as your NodeJS server file.
var data = require('/var/www/html/data.json')
;
var users = JSON.parse(data);
In the above case, require statement will automatically know that your file contains JSON data by looking at its extension .json.
3. Asynchronous JSON parsing
Both the above methods to parse JSON are synchronous. It means that if your JSON data is large it will take a long time to process and while this happens, your NodeJS server’s execution thread will not proceed further.
If you want to parse JSON asynchronously, then you will need to wrap it in a promise as shown below.
const parse_json = (json_string) => { return new Promise(resolve => { setTimeout(() => { resolve(JSON.parse(json_string)) }) }) } const data = '{ "name": "Joe", "age": 30 }' parse_json(data).then(json_data => console.log(json_data))
Similarly, if you want to asynchronously parse JSON data located in a file, then you need to use fs.readFile() function as shown below. Here also you need to provide full path to your json file, as shown in bold
const fs = require('fs') fs.readFile('/var/www/html/data.json', 'utf8', (err, file_contents) => { if (err) { console.error(err) return } try { const data = JSON.parse(file_contents) } catch(err) { console.error(err) } })
In this article, we have listed numerous ways for parsing JSON data. We have looked at how to read simple JSON strings and files synchronously. We have also looked at how to process JSON data asynchronously.
Please note, if your subsequent code depends on the value of parsed JSON data, then use synchronous parsing. If your JSON data is large or if you do not want to hold up the program execution, then only use asynchronous JSON parsing.
Also read:
How to Setup SSL/HTTPS in NodeJS Server
How to Hide PHP Version in WordPress/Apache
How to Append File in NodeJS
How to Run NodeJS App in Background
How to Fix EADDRINUSE in NodeJS
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.