9-bits
9-bits

Reputation: 10765

Django is throwing a "[123L, 123L] is not JSON serializable"

in my code i have:

json.dumps({'foo': {'bar': [123L, 123L]}})

the long ints are primary keys. this line for some reason generates the error:

"[123L, 123L] is not JSON serializable"

When i try to execute it in python manage.py shell:

json.dumps({'foo': {'bar': [123L, 123L]}})

works fine.

Can anyone tell me what's going on?

exact Django error:

Exception Type: TypeError Exception Value: [123L] is not JSON serializable Exception Location: /usr/lib/python2.7/dist-packages/simplejson/encoder.py in default, line 192

Upvotes: 0

Views: 864

Answers (1)

jdi
jdi

Reputation: 92569

This works for me:

from django.db.models.query import ValuesListQuerySet

def default(o):
    if isinstance(o, ValuesListQuerySet):
        return list(o)
    raise TypeError(repr(o) + " is not JSON serializable")

json.dumps(valueListObject, default=default)

Upvotes: 5

Related Questions