TheSayHeyKid
TheSayHeyKid

Reputation: 107

Python request to get JSON

This is my request.

h="API-AUTHENTICATION:key:secret"
r=requests.get("https://URL", h)

This is the response: <Response [200]>

If I print the plain text of the request (print(r.text)) I get this:

{
    "status": "OK",
    "data": [
        {
            "sort": 1,
            "parent_name": "Stocktake Demo",
            "timetables": [
                {
                    "active": 1,
                    "from_date": "Nov 01, 2019",
                    "timetable_data": {
                        "monday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "tuesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "friday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "wednesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "thursday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "sunday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "saturday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ]
                    },
                    "type": 1,
                    "to_date": ""
                }
            ],
            "name": "Stocktake Food",
            "parent_id": 137585,
            "parent_sort": 73,
            "image": null,
            "id": 137586,
            "description": null
        },
        {
            "sort": 1,
            "parent_name": "Main Category",
            "timetables": [
                {
                    "active": 1,
                    "from_date": "Nov 01, 2019",
                    "timetable_data": {
                        "monday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "tuesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "friday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "wednesday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "thursday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "sunday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ],
                        "saturday": [
                            {
                                "to": "23:59",
                                "from": "00:00"
                            }
                        ]
                    },
                    "type": 1,
                    "to_date": ""
                }
            ],
            "name": "Main Subcategory",
            "parent_id": 117042,
            "parent_sort": 2,
            "image": null,
            "id": 117043,
            "description": null
        }
    ]
}

If I do this:

a=json.loads(r.text)
print(a.keys())
print(a)

I get this:

dict_keys(['status', 'data'])

How to parse this into a dictionary and iterate it. Now when I iterate it I only reach status and data fields. I try to iterate it like this:

def print_depth(a, start=0):
    for key, value in a.items():
        print(key, start + 1)
        if isinstance(value, dict):
            print_depth(value, start=start + 1)
print_depth(a)

Upvotes: 5

Views: 16692

Answers (2)

Shijith
Shijith

Reputation: 4872

response.json() will give you json, if response contains a valid JSON.
You can check the 'content type' in the header of the response payload to verify whether response is a json or not using response.headers.get('Content-Type')

import requests
response = requests.get('https://api.github.com')
if response.status_code == 200 and 'application/json' in response.headers.get('Content-Type',''):
    print(response.json())

your response is a json with 2 keys status and data, you can access them directly from the json and iterate on data value which is a list

h="API-AUTHENTICATION:key:secret"
r=requests.get("https://URL", h)
if r.json().get('status') == 'OK':
    for item in r.json().get('data'):
        print(item.items())

Upvotes: 9

luigigi
luigigi

Reputation: 4215

Try:

import json
import requests
url = 'https://..........'
response = requests.get(url)
if response.status_code == 200:
    my_dict = json.loads(response.content.decode('utf-8'))

Upvotes: 2

Related Questions