This content originally appeared on DEV Community and was authored by Max
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
This content originally appeared on DEV Community and was authored by Max
Max | Sciencx (2023-03-08T14:35:17+00:00) Read and Write JSON in Python Requests from API. Retrieved from https://www.scien.cx/2023/03/08/read-and-write-json-in-python-requests-from-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.