0

So I'm trying to access the following JSON data with python and when i give the statement :

print school['students'] 

The underlying data gets printed but what I really want to be able to do is print the 'id' value.

   { 'students':[  
                  {  
                     'termone':{  
                        'english':'fifty',
                        'science':'hundred'
                     },
                     'id':'RA1081310005'
                  }
               ]
   }

So when I do the following I get an error :

 print school ['students']['id']

TypeError: list indices must be integers, not str

Can anyone suggest how i can access the ID & where I'm going wrong!

1
  • 1
    students is a list of students, not a dictionnary. so school["students"][0]['id'] Commented Dec 12, 2016 at 23:15

2 Answers 2

2

school['students'] is a list. You are trying to access the first element of that list and id key belongs to that element. Instead, try this:

school['students'][0]['id']
Out: 'RA1081310005'
Sign up to request clarification or add additional context in comments.

Comments

0

The problem here is that in your list, 'id' is not a part of a dictionary, it is part of a list. To fix this, change your dictionary to the following:

school = {'students':{
            'termone': {
                "english": "fifty:,
                "science": "hundred
            },
            "id":"RA1081310005"
        }
}

Basically, you have a list, and there is no reason to have it, so I removed it.

Comments

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.