Python read JSON from URL requests
In this article, you will learn how to read JSON from URL requests using the Python programming language.
JSON (JavaScript Object Notation) is a lightweight, open standard, data-interchange format. It is easy to read and write for humans. It is used primarily to transmit data between a web application and a server. Today, it is more popular than XML.
In this article, we have imported two modules- JSON and urllib.request. The JSON module is an in-built package of Python and is used to work with JSON data. It offers a function json.loads to parse the JSON string. The json.loads() method takes the file contents as a string.
The Python standard library provides utilities to read JSON files from URL resources. The urllib.request module is used for fetching URLs. It is capable to fetch a variety of data from different protocols as it allows us to make HTTP as well as HTTPS requests. It is also capable of providing complex interfaces, like basic authentication, cookies, proxies, and so on. It offers a function urlopen() which returns a response object for the URL requested.
Here is the simplest way to read the URL requests-
import urllib.request
response = urllib.request.urlopen('https://www.etutorialspoint.com/')
html = response.read()
Python read the JSON data from the requested URL
First, we define a function to read the JSON data from the requested URL. Within this function, we will open the URL using the urllib.request.urlopen() method. The response.getcode() returns the HTTP status code of the response. If it is 200, then read the JSON as a string, else print the error message.
def getData(url):
response = urllib.request.urlopen(url)
if(response.getcode()==200):
data = response.read()
jsonData = json.loads(data)
else:
print("Error occured", response.getcode())
return jsonData
Print the JSON Data
Next, we define a main function to print the JSON data. As we know, JSON objects are written in key/value pairs and the keys and values are separated by a colon. We can loop through object properties by using the for-in loop. In the for loop, we can use the bracket notation to access the property values. In this, we loop over the JSON response and print the country name and its capital.
def main():
url = "https://www.etutorialspoint.com/countries.json"
data = getData(url)
# print the country name and capital
for i in data["countries"]:
print(f'Name: {i["country"]["country_name"]}, Capital: {i["country"]["capital"]}')
Complete Code: Python read JSON from url
We have merged the above codes to get the complete code to read the JSON from the requested URL.
import urllib.request
import json
def getData(url):
response = urllib.request.urlopen(url)
if(response.getcode()==200):
data = response.read()
jsonData = json.loads(data)
else:
print("Error occured", response.getcode())
return jsonData
def main():
url = "https://www.etutorialspoint.com/countries.json"
data = getData(url)
# print the country name and capital
for i in data["countries"]:
print(f'Name: {i["country"]["country_name"]}, Capital: {i["country"]["capital"]}')
if __name__ == '__main__':
main()
When we run the above code, the output will look like this-
Related Articles
Convert MySQL query result to JSON in PythonInsert JSON data into MongoDB using Python
Convert MongoDB Document to JSON using Python
Python read JSON from URL requests
Python convert XML to JSON
Convert JSON to CSV using Python
Python JSON Tutorial - Create, Read, Parse JSON
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml