1

I am trying to send the JSON Format with added headers in .csv format to a front end for download. While sending a HTTP Response I am facing an "is not JSON serializable" error.

My views.py file:

from datetime import datetime
from django.shortcuts import render
from django.http import HttpResponse
import json as simplejson
import random
import csv

def DownloadEventLog(request):
    downloadeventlog = "[{\"severity\":\"0\",\"description\":\"USB Connected\",\"date\":\"01/01/2015\",\"time\":\"11:35:20\"},{\"severity\":\"3\",\"description\":\"USB Disconnected\",\"date\":\"01/01/2015\",\"time\":\"10:30:19\"}]";

    data = simplejson.loads(downloadeventlog)
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="eventlog.csv"'

    writer = csv.writer(response)
    writer.writerow(data[0].keys())
    for row in data:
        writer.writerow(row.values())
    print response
    return HttpResponse(simplejson.dumps(response), content_type = "application/json")

Print response cmd is printing:

Content-Type: text/csv
Content-Disposition: attachment; filename="eventlog.csv"

date,time,severity,description
01/01/2015,11:35:20,0,"USB Connected"
02/02/2015,10:30:19,3,"USB Disconnected"

However the last line is throwing an error as follows:

TypeError at /eventlog/downloadeventlog
<django.http.response.HttpResponse object at 0x9c059ec>is not JSON serializable

Request Method: POST
Request URL: http://127.0.0.1:8001/eventlog/downloadeventlog
Django Version: 1.7.1
Python Version: 2.7.3
sthzg
  • 5,514
  • 2
  • 29
  • 50
Dark Coder
  • 21
  • 6
  • possible duplicate of [ is not JSON serializable](http://stackoverflow.com/questions/16790375/django-object-is-not-json-serializable) – HassenPy Apr 22 '15 at 13:14

3 Answers3

4

simplejson and json don't work with django objects well.

Django's built-in serializers can only serialize querysets filled with django objects:

**** To use ****

data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, mimetype="application/json")

Hope that helps

Mushahid Khan
  • 2,816
  • 1
  • 19
  • 32
3

You can just return the response, you do not need the wrap it in HttpResponse.

return response
shellbye
  • 4,620
  • 4
  • 32
  • 44
  • True, if it is only for download purpose, I think you should use StreamingHttpReponse() when defining response. – abrunet Apr 22 '15 at 13:36
  • The browser will find that you returned a downloadable file by `Content-Disposition: attachment; filename="eventlog.csv"`, this is not the front end's job – shellbye Apr 23 '15 at 07:00
  • Yes, it worked and thanks i need not to send it as a HttpResponse at all. I just wanted to send the response to frontend make the file downloadable. – Dark Coder Apr 23 '15 at 07:21
  • Don't forget to accept my answer if it helps, I am working on my reputation recently. ^_^ – shellbye Apr 23 '15 at 07:26
1

You should use django's serializers here:
https://docs.djangoproject.com/en/dev/topics/serialization/

related question here:
<Django object > is not JSON serializable

Community
  • 1
  • 1
HassenPy
  • 2,083
  • 1
  • 16
  • 31