Read and Write JSON in Python Requests from API

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 pars…


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)

python requests get method data from response

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Read and Write JSON in Python Requests from API." Max | Sciencx - Wednesday March 8, 2023, https://www.scien.cx/2023/03/08/read-and-write-json-in-python-requests-from-api/
HARVARD
Max | Sciencx Wednesday March 8, 2023 » Read and Write JSON in Python Requests from API., viewed ,<https://www.scien.cx/2023/03/08/read-and-write-json-in-python-requests-from-api/>
VANCOUVER
Max | Sciencx - » Read and Write JSON in Python Requests from API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/08/read-and-write-json-in-python-requests-from-api/
CHICAGO
" » Read and Write JSON in Python Requests from API." Max | Sciencx - Accessed . https://www.scien.cx/2023/03/08/read-and-write-json-in-python-requests-from-api/
IEEE
" » Read and Write JSON in Python Requests from API." Max | Sciencx [Online]. Available: https://www.scien.cx/2023/03/08/read-and-write-json-in-python-requests-from-api/. [Accessed: ]
rf:citation
» Read and Write JSON in Python Requests from API | Max | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.