Reputation: 10554
I have a JSON string that I compute from a Pandas dataframe
aggr.aggregated.to_json(orient='values')
I cannot directly provide aggr.aggregated
to a standard Python JSON serializer because it would not follow the orient='values'
rules and would do so differently.
I want to serve my own JSON string as a response from a Django view:
return JsonResponse(aggr.aggregated.to_json(orient='values'))
However, in the code above, Django tried to serialize my JSON string.
How can I use JsonResponse
exclusively to set the Content-Type header to application/json but not to serialize a string that is already serialized?
Upvotes: 0
Views: 152
Reputation: 31514
There is no benefit in using JsonResponse
if you don't want it to encode the JSON for you.
Just use HttpResponse
and set the content-type header yourself:
return HttpResponse(
aggr.aggregated.to_json(orient='values'),
content_type='application/json'
)
Upvotes: 2