Search code examples
pythonjsonpython-3.xapipprint

Cannot find variables in a list of dictionaries


I am trying to ge around with APIs in general. To test this I coded this little snippet of code to get a list of all the channels on the Swedish national public service radio, and I want to print the ID and NAME of the channels:

import requests as rq
import json
from pprint import pprint

resp = rq.get('http://api.sr.se/api/v2/channels?
    format=json&indent=TRUE')

respjson = json.loads(resp.text)

pprint (respjson['id'])

And I get the error

File "sr-api.py", line 9, in <module>
pprint (respjson['id']['name'])
KeyError: 'id'

The (abbreviated) 'respjson' looks like this

{'channels': [{'channeltype': 'Rikskanal',
           'color': '31a1bd',
           'id': 132,
           'image': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg?preset=api-default-square',
           'imagetemplate': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg',
           'liveaudio': {'id': 132,
                         'statkey': '/app/direkt/p1[k(132)]',
                         'url': 'http://sverigesradio.se/topsy/direkt/srapi/132.mp3'},
           'name': 'P1',
           'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=132',
           'siteurl': 'http://sverigesradio.se/p1',
           'xmltvid': 'p1.sr.se'},
{'channeltype': 'Lokal kanal',
           'color': 'c31eaa',
           'id': 200,
           'image': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg?preset=api-default-square',
           'imagetemplate': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg',
           'liveaudio': {'id': 200,
                         'statkey': '/app/direkt/p4 jämtland[k(200)]',
                         'url': 'http://sverigesradio.se/topsy/direkt/srapi/200.mp3'},
           'name': 'P4 Jämtland',
           'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=200',
           'siteurl': 'http://sverigesradio.se/jamtland/',
           'xmltvid': 'p4jmtl.sr.se'}],
'copyright': 'Copyright Sveriges Radio 2017. All rights reserved.',
'pagination': {'nextpage': 'http://api.sr.se/v2/channelsformat=json&indent=true&page=2',
            'page': 1,
            'size': 10,
            'totalhits': 55,
            'totalpages': 6}}

Solution

  • Channels is a list. You have to iterate on it to get all channels and print their ids.

    # starting from respjson
    respjson = {
        'channels': [
            {
                'channeltype': 'Rikskanal',
                'color': '31a1bd',
                'id': 132,
                'image': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg?preset=api-default-square',
                'imagetemplate': 'http://static-cdn.sr.se/sida/images/132/2186745_512_512.jpg',
                'liveaudio': {'id': 132,
                              'statkey': '/app/direkt/p1[k(132)]',
                              'url': 'http://sverigesradio.se/topsy/direkt/srapi/132.mp3'},
                'name': 'P1',
                'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=132',
                'siteurl': 'http://sverigesradio.se/p1',
                'xmltvid': 'p1.sr.se'},
            {
                'channeltype': 'Lokal kanal',
                'color': 'c31eaa',
                'id': 200,
                'image': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg?preset=api-default-square',
                'imagetemplate': 'http://static-cdn.sr.se/sida/images/200/2186775_512_512.jpg',
                'liveaudio': {'id': 200,
                              'statkey': '/app/direkt/p4 jämtland[k(200)]',
                              'url': 'http://sverigesradio.se/topsy/direkt/srapi/200.mp3'},
                'name': 'P4 Jämtland',
                'scheduleurl': 'http://api.sr.se/v2/scheduledepisodes?channelid=200',
                'siteurl': 'http://sverigesradio.se/jamtland/',
                'xmltvid': 'p4jmtl.sr.se'
            }
        ],
        'copyright': 'Copyright Sveriges Radio 2017. All rights reserved.',
        'pagination': {
            'nextpage': 'http://api.sr.se/v2/channelsformat=json&indent=true&page=2',
            'page': 1,
            'size': 10,
            'totalhits': 55,
            'totalpages': 6
        }
    }
    
    for channel in respjson['channels']:
        print(channel['id'])