Read and Write JSON in Python Requests from API

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

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

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
35-websites-like-chaturbate:-the-most-effective-free-and-paid-alternate-options-better-than-chaturbate

35 Websites Like Chaturbate: The Most Effective Free And Paid Alternate Options Better Than Chaturbate

Next Post
how-to-give-writers-constructive-feedback-when-requesting-content-revisions

How To Give Writers Constructive Feedback When Requesting Content Revisions

Related Posts