xpanta xpanta - 5 months ago 55
JSON Question

django, "is not JSON serializable" when using ugettext_lazy?

I have this in my


response_dict = {
'status': status,
'message': message
return HttpResponse(simplejson.dumps(response_dict),

Since I start using this import:

from django.utils.translation import ugettext_lazy as _

at this line:

message = _('This is a test message')

I get this error:

File "/home/chris/work/project/prokject/main/views.py", line 830, in fooFunc
return HttpResponse(simplejson.dumps(response_dict),

File "/usr/local/lib/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)

File "/usr/local/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)

File "/usr/local/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)

File "/usr/local/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")

TypeError: <django.utils.functional.__proxy__ object at 0x7f42d581b590> is not JSON serializable

Why? What am I doing wrong?


It's not a string yet, and Python's JSON encoder doesn't know about ugettext_lazy, so you'll have to force it to become a string with something like

response_dict = {
    'status': status,
    'message': unicode(message)