1

I am trying to read the http response in python. the response is as below and I need to extract all records out of the specific fields. Below is my json output, i have trimmed it. But I am getting type error. I am new to python not sure what I am missing here.

TypeError: list indices must be integers, not str

My code:

data={u'Movies': [{u'Movie': {u'movieID': u'30', u'status': u'released', u'aflag': u'1', u'Type': u'action', u'releasedate': u'1276624', u'moddate': u'1276636', u'name': u'titanic', u'frameflag': u'1', u'class': u'UA'}},{u'Movies': [{u'Movie': {u'movieID': u'32', u'status': u'released', u'aflag': u'0', u'Type': u'action', u'releasedate': u'1276624', u'moddate': u'1276636', u'name': u'movie2', u'frameflag': u'1', u'class': u'UA'}}


data_j=json.loads(data)
for i in data_j['Movies']['data_j']:
    print i['Movie']['name']
1
  • 1
    Your data variable is already a Python dictionary. It is not a JSON value. Did you give us the djata_j value instead? Commented Oct 7, 2015 at 11:03

1 Answer 1

2

data['Movies'] is a list of dictionaries. Loop over that list; there is no data_j index in the list:

for movie in data_j['Movies']:
    print movie['Movie']['name']
Sign up to request clarification or add additional context in comments.

2 Comments

Besides, data is already a dictionary, so I don't see how it will pass through json.loads.
@bereal: nope, the MCVE is anything but. But the error message tells us the data_j value is what is shown as data instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.