Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: string indices must be integers (Python) [duplicate]

Tags:

python

json

I am trying to retrieve the 'id' value : ad284hdnn.

I am getting the following error : TypeError: string indices must be integers

data = response.json()
print data

for key in data['result']:
     print key['id']

Here is the json that is returned when print the data string.

{u'meta': {u'httpStatus': u'200 - OK', u'requestId': u'12345'}, u'result': {u'username': u'[email protected]', u'firstName': u'joe', u'lastName': u'bloggs', u'accountStatus': u'active', u'id': u'ad284hdnn'}}
like image 529
Martin Avatar asked Apr 06 '18 15:04

Martin


1 Answers

data['result'] is a dictionary. Iterating over dict means iterating over its keys. Therefore key variable stores a string. That's why key['id'] raises TypeError: string indices must be integers.

like image 168
radzak Avatar answered Oct 20 '22 20:10

radzak