Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Parse a JSON response using Python requests library
    python

    Parse a JSON response using Python requests library

    April 7, 2023No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Use the Python requests module to process a JSON response.

    Here, we’ll go through the steps necessary to parse a JSON response utilizing the

    calls for the library. Let’s have a look at how to parse JSON data in Python by using the requests package to make a RESTful GET call and get a response in JSON format.

    To make it easier to work with JSON data, we’ll convert the response to JSON into a Python dictionary. JSON may be prettyPrinted into a human-readable format as well.

    A payload is the information that was returned in response to a GET request. The message content contains this data for our perusal. To inspect payload in its many incarnations, use the Response object’s properties and methods.

    The following three queries provide us access to payload data:

    module.


    • response.content

      used to access payload data in raw bytes format.

    • response.text

      : used to access payload data in String format.

    • response.json()

      used to access payload data in the

      JSON serialized format

      .

    The JSON Response Content

    When working with JSON data, we may make use of the in-built JSON decoder that is provided by the requests module. It’s as simple as calling response.json(). Using the key-value pair notation of Python, response.json() outputs JSON in dictionary format.

    If the JSON decoding fails, a 204 error may be returned. In the following, response.json() throws an error:

    scenario.

    • The response doesn’t contain any data.
    • The response contains invalid JSON

    Since a successful call to response.json() does not signify the success of the request, you must verify response.raise for status() or response.status code before parsing JSON.

    Some servers, after encountering an HTTP 500 error, may instead return a JSON object (e.g., error details with HTTP 500). Hence, after verifying response.raise for status() or response.status code, you should perform response.json(). This is a working example of using response.json() to parse JSON data.

    Here, a GET request is being made through httpbin.org. The httpbin.org website serves as a web service that accepts test requests and provides information in response. You may put your knowledge to the test with this

    code.

    import requests
    from requests.exceptions import HTTPError
    try:
        response = requests.get('https://httpbin.org/get')
        response.raise_for_status()
        # access JSOn content
        jsonResponse = response.json()
        print("Entire JSON response")
        print(jsonResponse)
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')
    except Exception as err:
        print(f'Other error occurred: {err}')

    Produce Results

    :

    Entire JSON response
    {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.21.0'}, 'origin': '49.35.214.177, 49.35.214.177', 'url': 'https://httpbin.org/get'}

    Repeatedly Return a JSON Object

    Let’s check out how to loop over all of the key-value combinations in a JSON file.

    one-by-one.

    print("Print each key-value pair from JSON response")
        for key, value in jsonResponse.items():
            print(key, ":", value)

    Produce Results:

    Print each key-value pair from JSON response
    args : {}
    headers : {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.21.0'}
    origin : 49.35.214.177, 49.35.214.177
    url : https://httpbin.org/get

    Use the key name to get the corresponding JSON key from the response.

    print("Access directly using a JSON key name")
    print("URL is ")
    print(jsonResponse["url"])

    Produce Results

    URL is 
    https://httpbin.org/get

    Retrieve Nested JSON key from response.

    print("Access nested JSON keys")
    print("Host is is ")
    print(jsonResponse["headers"]["Host"])

    Produce Results

    :

    Access nested JSON keys
    URL is 
    httpbin.org
    Learn Python free Parse a JSON response using Python requests library Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePython Library Download
    Next Article Dictionaries and Arrays Selecting the Ideal Data Structure

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.