parsing json output

Justin Ezequiel justin.mailinglists at gmail.com
Wed Mar 19 04:08:11 CET 2008


FWIW, using json.py I got from somewhere forgotten,

>>> import json
>>> url = 'http://cmsdoc.cern.ch/cms/test/aprom/phedex/dev/gowri/datasvc/tbedi/requestDetails'
>>> params = {'format':'json'}
>>> import urllib
>>> eparams = urllib.urlencode(params)
>>> import urllib2
>>> request = urllib2.Request(url,eparams)
>>> response = urllib2.urlopen(request)
>>> s = response.read()
>>> len(s)
115337
>>> s[:200]
'{"phedex":{"request":
[{"last_update":"1188037561","numofapproved":"1","id":"7425"},
{"last_update":"1188751826","numofapproved":"1","id":"8041"},
{"last_update":"1190116795","numofapproved":"1","id":"92'
>>> x = json.read(s)
>>> type(x)
<type 'dict'>
>>> x.keys()
['phedex']
>>> type(x['phedex'])
<type 'dict'>
>>> x['phedex'].keys()
['request_date', 'request_timestamp', 'request', 'call_time',
'instance', 'request_call', 'request_url']


##    json.py implements a JSON (http://json.org) reader and writer.
##    Copyright (C) 2005  Patrick D. Logan
##    Contact mailto:patrickdlogan at stardecisions.com
##
##    This library is free software; you can redistribute it and/or
##    modify it under the terms of the GNU Lesser General Public
##    License as published by the Free Software Foundation; either
##    version 2.1 of the License, or (at your option) any later
version.
##
##    This library is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU
##    Lesser General Public License for more details.
##
##    You should have received a copy of the GNU Lesser General Public
##    License along with this library; if not, write to the Free
Software
##    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307  USA



More information about the Python-list mailing list