3

I want to return the queryset in json format, and I use the JsonResponse as the following:

def all_alert_history(request):
''' get all all alert history data '''
    all_data_json = serializers.serialize('json', LatestAlert.objects.all())
    return JsonResponse(all_data_json,safe=False)

but the browser shows like this:

"[{\"fields\": {\"alert_name\": \"memory usage\", \"alert_value\": 83.7, \"alert_time\": \"2016-11-08T06:21:20.717Z\", \"alert_level\": \"warning\", \"alert_rule\": \"warning: > 80%\"}, \"model\": \"alert_handler.latestalert\", \"pk\": \"xyz.test-java.ip-10-0-10-138.memory.percent\"}]"

I replace the JsonResponse with HttpResponse :

def all_alert_history(request):
''' get all all alert history data '''
all_data_json = serializers.serialize('json', LatestAlert.objects.all())
return HttpResponse(all_data_json, content_type='application/json') 

and the browser shows like this:

[{"fields": {"alert_name": "memory usage", "alert_value": 83.7, "alert_time": "2016-11-08T06:21:20.717Z", "alert_level": "warning", "alert_rule": "warning: > 80%"}, "model": "alert_handler.latestalert", "pk": "xyz.test-java.ip-10-0-10-138.memory.percent"}]

so, why does the \ appears when I use the JsonResponse but disappear when use the HttpResponse?

django version:1.8

1 Answer 1

1

JsonResponse takes a python dictionary and returns it as a json formatted string for the browser.

Since you're providing the JsonResponse with an already json formatted string it will try to escape all necessary characters with \.

Example:

>>> from django.http import JsonResponse
>>> response = JsonResponse({'foo': 'bar'})
>>> response.content
b'{"foo": "bar"}'

In your case JsonResponse even warns you about what you are doing when passing a string, hence making the safe = False parameter necessary:

>>> mydata = {"asd":"bdf"}
>>> import json
>>> myjson = json.dumps(mydata)
>>> JsonResponse(myjson)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/swozny/work2/local/lib/python2.7/site-packages/django/http/response.py", line 500, in __init__
    raise TypeError('In order to allow non-dict objects to be '
TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False

With the parameter set to False your observed behavior is reproducible:

>>> JsonResponse(myjson,safe=False).content
'"{\\"asd\\": \\"bdf\\"}"'

Bottom line is that if your model is a little more complex than basic data types ( IntegerField,CharField,...) then you probably will want to do the serialization yourself and stick to HttpResponse or just use djangorestframework which offers tools to do it for you.

Sign up to request clarification or add additional context in comments.

3 Comments

but if I use the queryset directly in JsonResponse it will get an error: TypeError at /all_history [<LatestAlert: memory usage>, <LatestAlert: memory usage>] is not JSON serializable. the LatestAlert.objects.all() is a queryset, I don't know how to convert it into a dict. I'm a newcomer to django
Yeah that's why you stick to all_data_json = serializers.serialize('json', LatestAlert.objects.all()) return HttpResponse(all_data_json, content_type='application/json')
Accept the answer please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.