Home > Blockchain >  json dictionary TypeError: string indices must be integers
json dictionary TypeError: string indices must be integers

Time:07-31

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":"[email protected]","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?

CodePudding user response:

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"])
  • Related