Python requests module allows you to send HTTP requests, The response from the HTTP request will have content, status, header meta details.
Using the requests package we can make a get request to a API which will return a JSON response it will be parse as python dictionary data.
getData function is used to make api call and parse data, we can check the response status by checking the status_code from response. And function writeData is used to write the response as json to local storage.
import json
import requests
def getData():
url = "https://dummyjson.com/products/1"
r = requests.get(url)
print("Status:", r.status_code)
return r.json()
def writeData(data):
with open("jsonfile.json", "w") as file:
json.dump(data, file, indent=4)
data = getData()
writeData(data)
Explore Other Related Articles
Read and Write Python Json
How to combine two dictionaries in python using different methods
How to check if a key exists in a dictionary python
Python try exception tutorial
Python classes and objects tutorial
Python Recursion Function Tutorial
Top comments (0)