Jan Bouchner
Jan Bouchner

Reputation: 897

parse JSON values by multilevel keys

Yesterday, I have started with learning python. I want to parse some JSON values now. I have read many of tutorials and spent a lot of time on getting values by multilevel key (if I can call it like that) in my script but nothing works to me. Can you help me please?

This is my JSON output:

{
"future.arte.tv": [
    {
        "mediaUrl": "http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR",
        "micropost": {
            "html": "Berlin ",
            "plainText": "Berlin"
        },
        "micropostUrl": "http://future.arte.tv/de/der-erste-weltkrieg-die-rolle-von-wissenschaft-und-technik",
        "publicationDate": "Tue Jun 17 20:31:33 CEST 2014",
        "relevance": 5.9615083,
        "timestamp": 1403029893606,
        "type": "image"
    }
],
"www.zdf.de": [
    {
        "mediaUrl": "http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025",
        "micropost": {
            "plainText": "Berlin direkt"
        },
        "micropostUrl": "http://www.zdf.de/ZDFmediathek/hauptnavigation/sendung-a-bis-z",
        "publicationDate": "Tue Jun 10 16:25:42 CEST 2014",
        "relevance": 3.7259426,
        "timestamp": 1402410342400,
        "type": "image"
    }
]
}

I need to get values stored in "mediaUrl" key so I tried to do

j = json.loads(jsonOutput)
keys = j.keys(); 
for key in keys:
    print key   # keys are future.arte.tv and www.zdf.de
    print j[key]["mediaUrl"]

but print j[key]["mediaUrl"] causes this error:

TypeError: list indices must be integers, not str

so I tried to do print j[key][0] but the result is not as I wanted to have (I want to have just mediaUrl value... btw j[key][1] causes list index out of range error):

{u'micropostUrl': u'http://www.berlin.de/special/gesundheit-und-beauty/ernaehrung/1692726-215-spargelhoefe-in-brandenburg.html', u'mediaUrl': u'http://berlin.de/binaries/asset/image_assets/42859/ratio_4_3/1371638570/170x130/', u'timestamp': 1403862143675, u'micropost': {u'plainText': u'Spargel', u'html': u'Spargel '}, u'publicationDate': u'Fri Jun 27 11:42:23 CEST 2014', u'relevance': 1.6377668, u'type': u'image'}

Can you give me some advice please?

Upvotes: 1

Views: 7494

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

Here is a list comprehension that should do it

>>> [d[i][0].get('mediaUrl') for i in d.keys()]
['http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025',
 'http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR']

How it works

First you can get a list of the top-level keys

>>> d.keys()
['www.zdf.de', 'future.arte.tv']

Get the corresponding values

>>> [d[i] for i in d.keys()]
[[{'micropostUrl': 'http://www.zdf.de/ZDFmediathek/hauptnavigation/sendung-a-bis-z', 'mediaUrl': 'http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025', 'timestamp': 1402410342400L, 'micropost': {'plainText': 'Berlin direkt'}, 'publicationDate': 'Tue Jun 10 16:25:42 CEST 2014', 'relevance': 3.7259426, 'type': 'image'}], [{'micropostUrl': 'http://future.arte.tv/de/der-erste-weltkrieg-die-rolle-von-wissenschaft-und-technik', 'mediaUrl': 'http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR', 'timestamp': 1403029893606L, 'micropost': {'plainText': 'Berlin', 'html': 'Berlin '}, 'publicationDate': 'Tue Jun 17 20:31:33 CEST 2014', 'relevance': 5.9615083, 'type': 'image'}]]

For each dictionary, grab the value for the 'mediaUrl' key

>>> [d[i][0].get('mediaUrl') for i in d.keys()]
['http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025',
 'http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR']

Upvotes: 2

Related Questions