TypeError: ObjectId() is not JSON serializable

1,682 views
Skip to first unread message

Yannick

unread,
May 19, 2014, 6:40:46 PM5/19/14
to pylons-...@googlegroups.com
Hello mate,

I'm rendering my Python function to JSON. Inside the function I got data from mongodb as following:

@view_config(route_name='listdisplay', renderer='json')
def matchFactStat(request):
rs  = request.db.collection.find() 
        return list(playerMatchStatList)

When I do that I have a TypeError: ObjectId() is not JSON serializable

Can you please let me know how to fix this ?
Thanks

Yannick

unread,
May 19, 2014, 6:55:12 PM5/19/14
to pylons-...@googlegroups.com
Basically what I did I parse the resultset and for each row I convert the ObjectId to String and it works. 
But do you know a more efficient way to handle this situation ?

Randall Leeds

unread,
May 19, 2014, 7:06:20 PM5/19/14
to pylons-...@googlegroups.com

http://docs.pylonsproject.org/projects/pyramid/en/master/narr/renderers.html#using-the-add-adapter-method-of-a-custom-json-renderer

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Jonathan Vanasco

unread,
May 19, 2014, 11:01:45 PM5/19/14
to pylons-...@googlegroups.com


On Monday, May 19, 2014 7:06:20 PM UTC-4, Randall Leeds wrote:

http://docs.pylonsproject.org/projects/pyramid/en/master/narr/renderers.html#using-the-add-adapter-method-of-a-custom-json-renderer 

FWIW, I found it more beneficial to use a custom JSON serializer and make it available via a "py2json" function... then register a new "json" & "jsonp" renderer with pyramid that uses it.

The pyramid adapter method is perfectly fine, but at some point you may need to write (not just render) JSON objects too.

Michael Merickel

unread,
May 20, 2014, 1:34:59 AM5/20/14
to Pylons

On Mon, May 19, 2014 at 10:01 PM, Jonathan Vanasco <jona...@findmeon.com> wrote:
The pyramid adapter method is perfectly fine, but at some point you may need to write (not just render) JSON objects too.

I have no idea what you mean by writing vs rendering json objects but hopefully you know that you can do json_obj = pyramid.renderers.render('json', obj) from anywhere to get a json serialized string.

Jonathan Vanasco

unread,
May 20, 2014, 1:41:43 AM5/20/14
to pylons-...@googlegroups.com
long day...

i meant that at some point you may be using a variety of scripts/apps that somehow interact with your project (celery, twisted, etc).  If you rely on the pyramid rendering framework for this, you essentially become dependent on Pyramid for this.  

If you have a standalone package that can expose a python-to-json serializer ( and possibly a reader too ), it's super portable across all your code.
Reply all
Reply to author
Forward
0 new messages