2

Trying to get specific data through my JSON format and I'm unable to do so. Trying to do it in Python and I want more than 1 thing printed out. Im providing a example code to make it clear. The name of the file is blah

{ u'info': { u'more_info': { u'xyz': u'[]',
                           u'xyz': None,
                           u'xyz': 00000,
                           u'description': u'blah blah blah',
                           u'xyz': 0000,
                           u'url':
                           u'xyz': False,
                           u'name': u'BLAH BLAH',
                           u'xyz': 10,
                           u'xyz': 1,

I refer to the documentation, but that still fails me.

I tried this

for xyz in blah['info']['more_info']:
fs = xyz['name']['description']['url'] 

The error is that: "TypeError: String indices must be integer"

Please help.

1
  • Careful with your indentation. It's not the cause of the error you're seeing, but the line starting fs = should be indented. Commented Sep 29, 2015 at 23:16

1 Answer 1

1

Iterating over a dict yields keys. But since you have a dict, just access the items directly.

print blah[u'info'][u'more_info'][u'name']
print blah[u'info'][u'more_info'][u'description']
print blah[u'info'][u'more_info'][u'url']
Sign up to request clarification or add additional context in comments.

1 Comment

And to save repeated lookup costs (and typing), you can assign a local with the results of the first two lookups, more_info = blah[u'info'][u'more_info'] then use the cached value repeatedly, print more_info[u'name'], print more_info['description'], etc.

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.