Siddharth Hemrajani Siddharth Hemrajani - 4 months ago 11
JSON Question

Access JSON data in Python

header = {'Content-type': 'application/json','Authorization': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' }
url = 'https://sandbox-authservice.priaid.ch/login'
response = requests.post(url, headers = header, verify=False).json()
token = json.dumps(response)
print token['ValidThrough']


I want to print the ValidThrough Attribute in my webhook, which is received as JSON data via a POST call. I know this has been asked a number of times here, but print token['ValidThrough']isnt working for me.I receive the error "TypeError: string indices must be integers, not str"

Answer

Since the response already seems to be in json, there is no need to use json.dumps.

json.dumps on a dictionary will return a string which cannot be indexed obviously and hence that error.