Extracting json data for parsing from HTTP GET

1,870 views
Skip to first unread message

irum

unread,
Sep 2, 2010, 4:57:55 AM9/2/10
to Django users
Hi,
I am having problems with extracting json data for parsing from HTTP
GET request.

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
deserialize. 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

Daniel Roseman

unread,
Sep 2, 2010, 5:09:14 AM9/2/10
to Django users
json.loads() and dujmps() work on a string or string-like object.
HttpResponse is a file-like object, not a string, so you could either
do loads(p.read()), or - much better - use the load() and dump()
methods, without the s, directly on the HttpResponse.
--
DR.

irum

unread,
Sep 2, 2010, 5:37:56 AM9/2/10
to Django users
Hi,
Thanks for your prompt reply.
I have tried both the things but I get the same error.
With,
x = json.loads(p.read())
print x

I get following error: 'HttpResponse' object has no attribute
'read'
Also I get same error with, json.dumps(p.read()), and same when I use
loads and dumps as load and dump without s.

Then, I tried the other option,

x = json.dump(p)
print x

I get the following error: dump() takes at least 2 arguments (1 given)
What would be the second argument, I googled on this but find no
helpful tips as everything written is too complicated.

And with:
x = json.load(p)
print x

'HttpResponse' object has no attribute 'read'

In all the examples, p is the HttpResponse object I get.

Looking forward,
Irum

Daniel Roseman

unread,
Sep 2, 2010, 6:11:13 AM9/2/10
to Django users
On Sep 2, 10:37 am, irum <irumrauf...@gmail.com> wrote:
> Hi,
> Thanks for your prompt reply.
> I have tried both the things but I get the same error.
> With,
> x = json.loads(p.read())
>            print x
>
> I get following error:  'HttpResponse' object has no attribute
> 'read'
> Also I get same error with, json.dumps(p.read()), and same when I use
> loads and dumps as load and dump without s.
>
> Then, I tried the other option,
>
> x = json.dump(p)
>         print x
>
> I get the following error: dump() takes at least 2 arguments (1 given)
> What would be the second argument, I googled on this but find no
> helpful tips as everything written is too complicated.
>
> And with:
>            x = json.load(p)
>            print x
>
> 'HttpResponse' object has no attribute 'read'
>
> In all the examples, p is the HttpResponse object I get.
>
> Looking forward,
> Irum

Apologies for steering you wrong - that'll teach me to post without
checking.

You're right that HttpResponse doesn't have a read() method, contrary
to what I thought. So neither of those methods will work. Instead, get
the content of the response and call loads() on that:

json.loads(p.content)

--
DR.

irum

unread,
Sep 2, 2010, 7:19:37 AM9/2/10
to Django users
Thank you so much, it worked:)...Now I just have to play with parsing
json that I think I can manage.

Thanks again:)

Irum
Reply all
Reply to author
Forward
0 new messages