Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

parsing json output

 
 
Gowri
Guest
Posts: n/a
 
      03-19-2008
Hi,

I have a service running somewhere which gives me JSON data. What I do
is this:

import urllib,urllib2
import cjson

url = 'http://cmsdoc.cern.ch/cms/test/aprom/phedex/dev/gowri/datasvc/
tbedi/requestDetails'
params = {'format':'json'}
eparams = urllib.urlencode(params)
request = urllib2.Request(url,eparams)
response = urllib2.urlopen(request) # This request is sent in HTTP
POST
print response.read()

This prints a whole bunch of nonsense as expected. I use cjson and am
unable to figure out how to print this json response and I guess if I
can do this, parsing should be straightforward? doing a
cjson.decode(str(repsonse)) does not work. In what form is this data
returned and how do I print it and later pass it on to another client
browser? Typing
http://cmsdoc.cern.ch/cms/test/aprom...requestDetails
in my browser returns valid json data.

Regards,
Gowri
 
Reply With Quote
 
 
 
 
Justin Ezequiel
Guest
Posts: n/a
 
      03-19-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","i d":"7425"},
{"last_update":"1188751826","numofapproved":"1","i d":"8041"},
{"last_update":"1190116795","numofapproved":"1","i d":"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
##
## 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
 
Reply With Quote
 
 
 
 
Gowri
Guest
Posts: n/a
 
      03-19-2008
Thanks for your reply Justin. But how do I do it with cjson?
 
Reply With Quote
 
Gowri
Guest
Posts: n/a
 
      03-19-2008
I actually have a weirder problem. The code I posted earlier prints
garbage on Windows and python 2.5 and the perfect json data on RHEL
python 2.3.4. I'm so confused and helpless. json.py doesn't seem to
help either. It says

raise ReadException, "Input is not valid JSON: '%s'" %
self._generator.all()

Somebody please help!
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      03-19-2008
Gowri <> wrote:
>
>I have a service running somewhere which gives me JSON data.
>...
>This prints a whole bunch of nonsense as expected.


It's not nonsense. It's JSON.

>I use cjson and am
>unable to figure out how to print this json response


You are already printing the JSON response. That's what that script does.

>and I guess if I
>can do this, parsing should be straightforward? doing a
>cjson.decode(str(repsonse)) does not work.


If response.read() returns the JSON string, as you show above, one might
expect
cjson.decode(response.read())
to parse it. Have you read the cjson documentation?
--
Tim Roberts,
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Gowri
Guest
Posts: n/a
 
      03-20-2008
Hi Tim,

I understand it's JSON. My problem is that it just prints crazy
characters instead of the JSON data. Like I mentioned, this happens on
my windows machine which has python 2.5. On the other hand, the same
code worked perfectly great on my linux machine with python 2.3.4.
What could the problem be?

Regards,
Gowri

 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      03-21-2008
Gowri <> wrote:
>
>I understand it's JSON. My problem is that it just prints crazy
>characters instead of the JSON data.


Ah, I see. I didn't get that from your original post.

>Like I mentioned, this happens on
>my windows machine which has python 2.5. On the other hand, the same
>code worked perfectly great on my linux machine with python 2.3.4.
>What could the problem be?


I'm not sure. It worked correctly on my Windows machine with Python 2.4.4.
Are you going through a proxy? Are you able to read other (non-JSON) web
pages using urllib2?
--
Tim Roberts,
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Gowri
Guest
Posts: n/a
 
      03-24-2008
Hi Tim,

I'm able to get and print correctly some HTML content. I don't know
how to fix this. Need all the help I can get with this

Regards,
Gowri
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      03-24-2008
On Mar 18, 9:10*pm, Gowri <gowr...@gmail.com> wrote:
> Hi,
>
> I have a service running somewhere which gives me JSON data. What I do
> is this:
>
> import urllib,urllib2
> import cjson
>
> url = 'http://cmsdoc.cern.ch/cms/test/aprom/phedex/dev/gowri/datasvc/
> tbedi/requestDetails'
> params = {'format':'json'}
> eparams = urllib.urlencode(params)
> request = urllib2.Request(url,eparams)
> response = urllib2.urlopen(request) * *# This request is sent in HTTP
> POST
> print response.read()
>
> This prints a whole bunch of nonsense as expected. I use cjson and am
> unable to figure out how to print this json response and I guess if I
> can do this, parsing should be straightforward?

<snip>

Gowri -

On a lark, I tried using the JSON parser that ships with the examples
in pyparsing (also available online at http://pyparsing.wikispaces.com/spac...jsonParser.py).
The parsed data returned by pyparsing gives you a results object that
supports an attribute-style access to the individual fields of the
JSON object. (Note: this parser only reads, it does not write out
JSON.)

Here is the code to use the pyparsing JSON parser (after downloading
pyparsing and the jsonParser.py example), tacked on to your previously-
posted code to retrieve the JSON data in variable 's':


from jsonParser import jsonObject
data = jsonObject.parseString(s)

# dump out listing of object and attributes
print data.dump()
print

# printe out specific attributes
print data.phedex.call_time
print data.phedex.instance
print data.phedex.request_call

# access an array of request objects
print len(data.phedex.request)
for req in data.phedex.request:
#~ print req.dump()
print "-", req.id, req.last_update


This prints out (long lines clipped with '...'):

[['phedex', [['request', [[['last_update', '1188037561'], ...
- phedex: [['request', [[['last_update', '1188037561'],
['numofapproved', '1'],...
- call_time: 0.10059
- instance: tbedi
- request: [[['last_update', '1188037561'], ['numofapproved',
'1'], ...
- request_call: requestDetails
- request_date: 2008-03-24 12:56:32 UTC
- request_timestamp: 1206363392.09
- request_url: http://cmsdoc.cern.ch/cms/test/aprom...ls?format=json

0.10059
tbedi
requestDetails
1884
- 7425 1188037561
- 8041 1188751826
- 9281 1190116795
- 9521 1190248781
- 12821 1192615612
- 13121 1192729887
...

The dump() method is a quick way to see what keys are defined in the
output object, and from the code you can see how to nest the
attributes following the nesting in the dump() output.

Pyparsing is pure Python, so it is quite portable, and works with
Python 2.3.1 and up (I ran this example with 2.5.1).

You can find out more at http://pyparsing.wikispaces.com.

-- Paul
 
Reply With Quote
 
Gowri
Guest
Posts: n/a
 
      03-27-2008
Hi all,

Thank you so much for all your help I really appreciate it. I
discovered that my problem was because of my firewall and everything
works now

Regards,
Gowri
 
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
parsing json using simplejson deostroll Python 5 06-16-2009 04:31 AM
"JSON for ASP" at json.org Tuðrul Topuz ASP General 1 06-27-2008 11:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57