Parse JSON In Node.js From External URL

In one of my articles, I have described about How To Parse JSON In Node.js. In that article I have told some ways to parse JSON without parsing from external URL. Today I am going to share this method with you. Let’s follow these steps:

Table of Contents

  1. Install Request Package
  2. Create a JS File
  3. Run the Project

Step 1 : Install Request Package

Request package helps us to parse JSON from an external URL. Go to your project directory using CMD. Then type this command to install request package:

nmp install request

Step 2 : Create a JS File

We have downloaded the main necessary library for this project. Now create a file named “server.js” in the root directory of your project. Open the JS file with an editor and paste this code:

server.js
var request = require("request")

var url = "https://jsonplaceholder.typicode.com/posts/1"

request({
    url: url,
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {
        console.log(body) // Print the json response
    }
})

Step 3 : Run the Project

Our project is ready to run. Let’s run the project and see the output:

node server

When I run the project, I have seen this output:

We have successfully parsed JSON from the external URL.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.