python equivalent for JSONObject

17 views
Skip to first unread message

Pankaj Vishwani

unread,
Jul 2, 2008, 12:48:13 AM7/2/08
to Google App Engine
I am sure my question is the dummest question but I couldn't find any
link.
So I am trying to create a JSON Object in python and then send it as a
response.
In Java I would do the following:

JSONObject jObj = new JSONObject();
jObj.put("key", "value1");
response.out.write(jObj,toString());

Can someone tell me the equivalent for GAE + python.


Regards,
Pankaj Vishwani
pankajv...@gmail.com

David Symonds

unread,
Jul 2, 2008, 3:12:18 AM7/2/08
to google-a...@googlegroups.com
On Wed, Jul 2, 2008 at 2:48 PM, Pankaj Vishwani
<pankajv...@gmail.com> wrote:

> I am sure my question is the dummest question but I couldn't find any
> link.
> So I am trying to create a JSON Object in python and then send it as a
> response.

There's nothing built in to Python or included in GAE. I quite like
simplejson, which makes it really easy:

import simplejson
self.response.out.write(simplejson.dumps(my_data))


Dave.

Alex Dong

unread,
Jul 2, 2008, 6:31:01 AM7/2/08
to Google App Engine
I've had great experience with demjson.

Alex

On Jul 2, 3:12 pm, "David Symonds" <dsymo...@gmail.com> wrote:
> On Wed, Jul 2, 2008 at 2:48 PM, Pankaj Vishwani
>

Yagiz Erkan

unread,
Jul 2, 2008, 6:35:45 AM7/2/08
to google-a...@googlegroups.com
Well... Correct me if I'm wrong but as far as I remember, neither
demjson nor simplejson handle custom objects automatically. The
developer has to write code to specify serialization.

- Yagiz -

David Symonds

unread,
Jul 2, 2008, 8:23:25 AM7/2/08
to google-a...@googlegroups.com

That's correct -- there's no way for *any* JSON library to do that
automatically and correctly.


Dave.

Alex Dong

unread,
Jul 2, 2008, 9:38:27 AM7/2/08
to Google App Engine
A quick way to do so is to leverage web.py's Storage wrapper.
http://webpy.org/docs search for Storage

HTH
Alex

On Jul 2, 8:23 pm, "David Symonds" <dsymo...@gmail.com> wrote:

MH

unread,
Jul 2, 2008, 3:03:50 AM7/2/08
to Google App Engine
I use demjson.

http://deron.meranda.us/python/demjson/


response.out.write(demjson.encode({
"key" : "value"
}))

Pankaj Vishwani

unread,
Jul 3, 2008, 9:39:12 AM7/3/08
to Google App Engine

Problem solved. Yeah........ !!!!
I am not sure why but simplejson.dump(my_dict) didn't work for me and
was throwing some kind of exception but demjson worked on the first
attempt.
Thanks a lot guys for your help.
I really appreciate it.


Regards,
Pankaj Vishwani
pankajv...@gmail.com
> }))- Hide quoted text -
>
> - Show quoted text -

David Symonds

unread,
Jul 3, 2008, 6:21:58 PM7/3/08
to google-a...@googlegroups.com
On Thu, Jul 3, 2008 at 11:39 PM, Pankaj Vishwani
<pankajv...@gmail.com> wrote:

> Problem solved. Yeah........ !!!!
> I am not sure why but simplejson.dump(my_dict) didn't work for me and
> was throwing some kind of exception but demjson worked on the first
> attempt.

That would be because the function is called "dumps", not "dump".


Dave.

Pankaj Vishwani

unread,
Jul 3, 2008, 6:28:14 PM7/3/08
to Google App Engine
Sorry, about the typo but I did use "simplejson.dumps".


Regards,
Pankaj Vishwani


On Jul 3, 3:21 pm, "David Symonds" <dsymo...@gmail.com> wrote:
> On Thu, Jul 3, 2008 at 11:39 PM, Pankaj Vishwani
>
Reply all
Reply to author
Forward
0 new messages