1

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}}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    Yes. Why did you exped `['id']` to work? The pretty-printed output clearly shows there is no such key at the top-level... So you have to first do `['channels']` which will give you a list with *more `dict` objects*, then for each of those `dict` objects in the list, you need to access `'id'`. – juanpa.arrivillaga Nov 01 '17 at 22:17
  • Because I didn't know better :-) – Martin Nordberg Nov 01 '17 at 22:30

2 Answers2

0

What you want to do is look thru the dictionaries presented inside the channels, you can do that with the following...

for dic in respjson['channels']:
    pprint(dic['id'])
0TTT0
  • 1,288
  • 1
  • 13
  • 23
0

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'])
Elis Byberi
  • 1,422
  • 1
  • 11
  • 20