[Fixed]-Serializing result of a queryset with json raises error:

21👍

“I am trying to serialize a Python list…”
This is actually not quite the full story.
You are trying to serialize a ValuesListQuerySet.

>>> type(ids)
<class 'django.db.models.query.ValuesListQuerySet'>

You can either
1. convert to a Python list as mentioned in the other great answers, or
2. serialize just the IDs.

Django has a built-in way to serialize a QuerySet.
And you only want the IDs so you may use the fields kwarg.

from django.core import serializers
data = serializers.serialize('json', YourEntity.objects.all(), fields=('id',))

4👍

EDIT after reading the other answers

The original answer I gave (here below unabridged) was correct in diagnosing what the problem is (the argument passed to the json function is not a list). I am leaving it as it explains the debugging procedure (maybe of some use for other similar situations), but the new answers of both @Jacinda and @Adam are more “to the point”. In particular, the latter contains instructions on how to use a native django functionality to overcome the problem.

Original answer

Not 100% sure because I can’t replicate the problem on my system, but from the look of it, it seems to me that it’s a problem in the type/encoding of the data.

I would start by testing again your code by manually assign ids with:

ids = [335L, 468L, 481L, 542L, 559L, 567L, 609L]

(on my system your code works in this case). If it works for you too, then investigate what kind of object is ids when assigned via p.values_list('id',flat=True) [you can do that with type(ids)]. It might be that ids is an object whose representation is the same as a list, but that it’s not a list.

In this case, you could try typecasting: ids = list(p.values_list('id',flat=True)) before passing it to the json function but there is no guarantee that it will work (it depends if the returned value of p.values_list is iterable or not.

HTH in at least tracking down the issue!

👤mac

3👍

I encountered the same error and after a lot of confusion, finally the solution that worked for me is as follows in a single answer for the question asked by @afshin:

ids = p.values_list('id',flat=True)
ids_list = list(ids)
import json
json.dumps(ids_list)

2👍

The problem here is that values_list(), which is a django function (you probably should clarify this in your question) does not return a list.

>>> x = UserProfile.objects.values_list('employee_id', flat=True)
>>> type(x)
>>> <class 'django.db.models.query.ValuesListQuerySet'>

simplejson.dumps doesn’t know how to serialize this object. So like @mac suggested, you need to typecast this return value to a list.

Leave a comment