python 3.6 JsonResponse issue

So i recently migrated to Python 3.6 and Django 1.11 and my JsonResponse code looked like this:

   return JsonResponse({'status': '1'}) 

it worked fine but after the migration i started getting this error:

TypeError: Object of type 'bytes' is not JSON serializable

After printing the type of the data passed to JsonResponse i realized python 3.6 changed this from dict to byte. So i changed the code to make sure i was passing a dict.

I still get the same error after trying all of this:

    data = dict([('status', 0)])
    print(data)
    print(type(data))
    # print(type(json.dumps(data)))
    # data = {"status": '0'}
    # data = json.dumps(data)
    # json.dumps(data.decode("utf-8"))
    #response = json.JSONEncoder().encode({"status": 0}) 
    #JsonResponse(data, safe=False)
    # response = json.dumps(data)
    print(JsonResponse(data, safe=False))
    return JsonResponse(data, safe=False)

Prints:

    {'status': 0}
    <class 'dict'>
    <JsonResponse status_code=200, "application/json">

with the json.dumps options y get this error instead

AttributeError: 'str' object has no attribute 'get'

Any help would be much appreciated

Traceback

    Traceback (most recent call last):
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/handlers/base.py", line 131, in get_response
    response = middleware_method(request, response)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/middleware.py", line 58, in process_response
request.session.save()
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 81, in save
return self.create()
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 54, in create
self.save(must_create=True)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 83, in save
obj = self.create_model_instance(data)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 69, in create_model_instance
session_data=self.encode(data),
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 98, in encode
serialized = self.serializer().dumps(session_dict)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/signing.py", line 93, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
   TypeError: Object of type 'bytes' is not JSON serializable

Upvotes: 3

Views: 1433

Answers (1)

Alasdair
Alasdair

Reputation: 308889

The problem isn't return JsonResponse({'status': '1'}).

The traceback is showing you that the error occurs when Django tries to save the Django session.

You must be doing something like this in the view:

request.session['my_key'] = b'bytes'

For that example, you would have to decode the bytes object (or use a string instead):

request.session['my_key'] = b'bytes'.decode('utf-8')

Upvotes: 3

Related Questions