parsing json data

J

jujulj

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'},
...........................
 
J

Jon Clements

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.
 
E

Edward A. Falk

There's a json parsing library in 2.6. (Sadly, 2.6 is not out for
Ubuntu yet.)
 
J

John Bokma

There's a json parsing library in 2.6. (Sadly, 2.6 is not out for
Ubuntu yet.)

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2

on Ubuntu 9.04

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top