parsing json data

Discussion in 'Python' started by jujulj, Nov 27, 2009.

  1. jujulj

    jujulj Guest

    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'},
    ...........................
    jujulj, Nov 27, 2009
    #1
    1. Advertising

  2. jujulj

    Jon Clements Guest

    On 27 Nov, 13:52, jujulj <> 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.
    Jon Clements, Nov 27, 2009
    #2
    1. Advertising

  3. Edward A. Falk, Nov 30, 2009
    #3
  4. jujulj

    John Bokma Guest

    (Edward A. Falk) wrote:

    > 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
    John Bokma, Nov 30, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    241
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    338
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    254
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    260
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    166
    David Karr
    Jun 17, 2013
Loading...

Share This Page