Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

json dictionary TypeError: string indices must be integers

import requests
    url = "**************"

querystring = {"domain":"gmail.com","username":"random","server":"server-1","type":"real"}

headers = {
    "X-RapidAPI-Key": "******************",
    "X-RapidAPI-Host": "*****************"
}

response = requests.request("GET", url, headers=headers, params=querystring).text

response:

{"code":200,"msg":"OK","items":{"email":"vinhvivyanvinh72943@gmail.com","timestamp":1659215390}}

.

print(response['items'])

TypeError: string indices must be integers

Hello,
I do not encounter any problems when I use it in another dictionary.
How can I get email and timestamp into variables?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use .json() on request response:

response = requests.request(
    "GET", url, headers=headers, params=querystring
).json()                                             # <-- note the .json()

print(response["items"]["email"], response["items"]["timestamp"])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading