Parse JSON In Node.js From External URL

Published on April 23, 2019 43 sec read

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. ๐Ÿ™‚

Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.

Follow

4 Replies to “Parse JSON In Node.js From External URL”

      1. Thank you realy much, thats what i search for. If you could make a small tutorial it would be perfect! ๐Ÿ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *