deserializing json

140 views
Skip to first unread message

irum

unread,
Aug 20, 2010, 8:16:04 AM8/20/10
to Django users
Hi,
I am trying to parse json, loop through it and access its data.
At first I tried this:

for obj in serializers.deserialize("json", p):
a=2
response = HttpResponse()
response.status_code = 200
return response

Here 'p' has the json data returned to me by another view. I have
validated it in http://www.jsonlint.com/ and it does not have any
error.

This gave attribute error.
AttributeError at /bookings/52/cancel/
'HttpResponse' object has no attribute 'read'

The problem I am sure is not in HttpResponse object as it works fine
when I comment out the line accessing object of deserialized data i.e
for obj in serializers.deserialize("json", p)

I then tried writing this:

deserialized = serializers.deserialize("json",
self.request.raw_post_data)
d = list(deserialized)[0].object

and also this:

deserialized = serializers.deserialize("json",
self.request.raw_post_data)
d = list(deserialized).object

I again get same error. It works fine if the second line: put_bookmark
= list(deserialized)[0].object , is commented out. But, when I
uncomment it to access object of deserialized data I get the same
error.

After many trial and errors, I am convinced the problem is when I try
to access object of deserialized data. I also referred to this thread:
http://groups.google.com/group/django-users/browse_thread/thread/87bd6951b4aebd00/d8e0a547be1583a8?lnk=gst&q=deserialize#d8e0a547be1583a8
in the discussion group. In this thread also the problem trickles down
to access object of deserialzed data but no further solution is
available.

I don want to use simplejson. Can someone give me a solution to this
problem and help me fix this.

Thanks,
Irum


I get the attribute error:
'HttpResponse' object has no attribute 'read'

Bill Freeman

unread,
Aug 20, 2010, 10:43:09 AM8/20/10
to django...@googlegroups.com
Have you tried looking at the raw data to see if it is what you
expect (capture to a file or pdb.set_trace() is your friend)?

Have you tried (assuming relatively recent python, >= 2.6
I think) loads from the json module?

Bill

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Reinout van Rees

unread,
Aug 20, 2010, 2:33:47 PM8/20/10
to django...@googlegroups.com
On 08/20/2010 02:16 PM, irum wrote:
> Hi,
> I am trying to parse json, loop through it and access its data.
> At first I tried this:
>
> for obj in serializers.deserialize("json", p):
> a=2
> response = HttpResponse()
> response.status_code = 200
> return response

One small thing to try:
response = HttpResponse('')
instead of:
response = HttpResponse()

So: feed it an empty string.

As it complains about being unreadable, perhaps you should stuff
something into it. Just a pretty wild guess...

Reinout

--
Reinout van Rees - rei...@vanrees.org - http://reinout.vanrees.org
Programmer at http://www.nelen-schuurmans.nl
"Military engineers build missiles. Civil engineers build targets"

irum

unread,
Sep 2, 2010, 4:51:52 AM9/2/10
to Django users
Hi,
Sorry for late reply. I had to switch to another project so this work
got delayed.

Actually, what I am doing is that I am receiving json data over HTTP
via GET. After debugging much, I have realized that the data also has
HTTP headers and other information that serializer is not able to
serialize. How can I extract json data from the complete data
(including HTTP information)?

This is the data I receive, i.e. p, and I want to extract json from it
and deserialize it for parsing.

HTTP/1.0 200 OK Date: Thu, 02 Sep 2010 08:37:39 GMT Server: WSGIServer/
0.1 Python/2.6.4 Content-Type: application/json [{"pk": 33, "model":
"hbexample.payment", "fields": {"confirm": true, "pDate": "2010-08-31
10:42:19", "waiting": false, "amount": 33.0, "p_try": 0, "booking":
34}}]



I have tried json.loads and json.dumps on this data.

With json.loads(p), I get the following error: expected string or
buffer

With json.dumps(p), I get the following error:
<django.http.HttpResponse object at 0x150da50> is not JSON
serializable

How do I address this problem? Any help regarding this would be
helpful.


Thanks and Looking forward,
Irum



On Aug 20, 9:33 pm, Reinout van Rees <rein...@vanrees.org> wrote:
> On 08/20/2010 02:16 PM,irumwrote:
>
> > Hi,
> > I am trying to parse json, loop through it and access its data.
> > At first I tried this:
>
> >                 for obj in serializers.deserialize("json", p):
> >                          a=2
> >                  response = HttpResponse()
> >                  response.status_code = 200
> >                  return response
>
> One small thing to try:
> response = HttpResponse('')
> instead of:
> response = HttpResponse()
>
> So: feed it an empty string.
>
> As it complains about being unreadable, perhaps you should stuff
> something into it.  Just a pretty wild guess...
>
> Reinout
>
> --
> Reinout van Rees - rein...@vanrees.org -http://reinout.vanrees.org
> Programmer athttp://www.nelen-schuurmans.nl
Reply all
Reply to author
Forward
0 new messages