parsing json data

Jon Clements joncle at googlemail.com
Fri Nov 27 14:59:13 CET 2009


On 27 Nov, 13:52, jujulj <juj... at gmail.com> wrote:
> Hi,
>
> I get the data shown below from the json geonames web service.
> What's the best way to get the name value of the alternateNames with a
> given lang value?
> Do I have to loop in the array to find it?
>
> thanks
>
> {u'adminCode1': u'09',
>  u'adminName1': u'The Federal District',
>  u'adminName2': u'',
>  u'adminName3': u'',
>  u'adminName4': u'',
>  u'alternateNames': [{u'lang': u'ko', u'name': u'\uba55\uc2dc\ucf54 \uc2dc'},
>                      {u'lang': u'ja',
>                       u'name': u'\u30e1\u30ad\u30b7\u30b3\u30b7\u30c6\u30a3'},
>                      {u'lang': u'th',
>                       u'name':
> u'\u0e40\u0e21\u0e47\u0e01\u0e0b\u0e34\u0e42\u0e01\u0e0b\u0e34\u0e15\u0e35'},
>                      {u'lang': u'gl', u'name': u'Cidade de M\xe9xico'},
>                      {u'lang': u'pt', u'name': u'Cidade do M\xe9xico'},
>                      {u'lang': u'scn', u'name': u'Cit\xe0 d\xfb Messicu'},
>                      {u'lang': u'scn', u'name': u'Cit\xe0 d\xfb M\xe8ssicu'},
>                      ...........................

A simple list comprehension should do the trick:

[el['name'] for el in json_data['alternateName'] if el['lang'] ==
'??']

Jon.



More information about the Python-list mailing list