Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to read a JSON response from a link in Python?

  • Difficulty Level : Easy
  • Last Updated : 24 Feb, 2021

There is a huge amount of data available on the web and most of them are in form of (JavaScript Object Notation) JSON. But it is difficult for humans to directly read and use it. To resolve this problem in python we have different libraries which help us to read the JSON data fetched from the web. These libraries have objects and functions which helps to open the URL from the web and read the data.

To read a JSON response there is a widely used library called urllib in python. This library helps to open the URL and read the JSON response from the web. To use this library in python and fetch JSON response we have to import the json and urllib in our code, The json.loads() method returns JSON object. Below is the process by which we can read the JSON response from a link or URL in python.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Approach:

  • Import required modules.
  • Assign URL.
  • Get the response of the URL using urlopen().
  • Convert it to a JSON response using json.loads().
  • Display the generated JSON response.

Implementation:



Python3




# import urllib library
from urllib.request import urlopen
  
# import json
import json
# store the URL in url as 
# parameter for urlopen
  
# store the response of URL
response = urlopen(url)
  
# storing the JSON response 
# from url in data
data_json = json.loads(response.read())
  
# print the json response
print(data_json)


Output:

In this way, one can easily read a JSON response from a given URL by using urlopen() method to get the response and then use json.loads() to convert the response into a JSON object.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!