Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parsing json data

Reply
Thread Tools

parsing json data

 
 
jujulj
Guest
Posts: n/a
 
      11-27-2009
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'},
...........................
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      11-27-2009
On 27 Nov, 13:52, jujulj <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Edward A. Falk
Guest
Posts: n/a
 
      11-30-2009
There's a json parsing library in 2.6. (Sadly, 2.6 is not out for
Ubuntu yet.)

--
-Ed Falk, http://www.velocityreviews.com/forums/(E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      11-30-2009
(E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM
Parsing nested JSON data Marko Anastasov Ruby 5 11-24-2007 11:07 AM



Advertisments