Help needed with extracting json data

Hello. I wondered if someone could help me extract json data. The original data looked like this:

{
'message':'accurate',
'cod':'200',
'count':5,
'list':[
{
'id':4156404,
'name':'Gainesville',
'coord':{
'lat':29.6519,
'lon':-82.325
},
'main':{
'temp':81.81,
'pressure':1014,
'humidity':30,
'temp_min':80.6,
'temp_max':82.4
},
'dt':1524948900,
'wind':{
'speed':4.7
},
'sys':{
'country':'US'
},
'rain':None,
'snow':None,
'clouds':{
'all':1
},
'weather':[
{
'id':800,
'main':'Clear',
'description':'sky is clear',
'icon':'01d'
}
]
},

To get weather from this set, I coded in description = weather_json['list'][0]['weather']
which gave me this:
[{'id': 800, 'main': 'Clear', 'description': 'sky is clear', 'icon': '01d'}]
So far everything’s good… But, then when I tried to extract the description from this with description = weather_json['list'][0]['weather']['description'] I got an error message:
TypeError: list indices must be integers or slices, not set
What could I be doing wrong? Thanks!

‘weather’ is an array with one element. To drill deeper down to description you will need to use [0] before [‘description’].

It is easier to see the hierarchy if you indent the JSON with some type of tidy or beautifier tool.


                'weather': [
                {
                    'id': 800,
                    'main': 'Clear',
                    'description': 'sky is clear',
                    'icon': '01d'
                }]

2 Likes