HOWTO · Django

How to Create a JSON Response in Django

Learn how to create a JSON Response in Django.

On this page

Use Django’s JsonResponse to return JSON-serializable data from a view. It accepts a dictionary by default, and you can pass a list with safe=False. For model instances or a QuerySet, first choose and shape the fields your API should expose. The examples below cover function-based and class-based views, list responses, model conversion, and the lower-level HttpResponse option.

Return a JSON object with Django’s JsonResponse

A dictionary is the usual input for JsonResponse. Django serializes it and sets the response content type to JSON, so a view can return it directly. Here is a small data shape to use in either view style:

Run this example:

import json

data = {"name": "Ada", "active": True}
print(json.dumps(data, separators=(",", ":")))

Output:

{"name":"Ada","active":true}

Import JsonResponse from django.http. A function-based view can be defined as def profile(request): return JsonResponse({"name": "Ada", "active": True}). For a class-based view, also import View from django.views and define class ProfileView(View): get = lambda self, request: JsonResponse({"name": "Ada", "active": True}). These Django-specific view patterns require a configured project; the standard-library example above demonstrates the JSON data shape, not a Django request or response.

The dictionary default is useful because it encourages a named, extensible response shape. For example, an API can return {"name": "Ada", "active": True} today and add another key later without changing the top-level type. Keep the values JSON-serializable: strings, numbers, booleans, None, lists, and dictionaries are common choices.

Return a JSON array with JsonResponse

A list is also a valid JSON value, but JsonResponse expects a dictionary unless told otherwise. Set safe=False when the top-level value is a list:

return JsonResponse(["Django", "Python"], safe=False)

Use this form when the response itself is a collection. If clients need metadata such as a count or pagination details, return a dictionary containing the list instead, for example {"items": ["Django", "Python"], "count": 2}. safe=False changes the permitted top-level shape; it does not make arbitrary Python objects serializable.

Convert model and QuerySet data before returning JSON

A Django model instance and a QuerySet are not automatically JSON-serializable. Choose the fields that belong in the public response and convert each record to ordinary Python values before passing the result to JsonResponse. This also prevents accidental exposure of internal model fields.

For example, a view can fetch one Blog and several blogs, then shape them explicitly:

one = Blog.objects.get(pk=1)

many = Blog.objects.all()

one_data = {"title": one.title, "likes": one.likes}

many_data = [{"title": blog.title, "likes": blog.likes} for blog in many]

return JsonResponse({"blog": one_data, "blogs": many_data})

This is a focused shaping pattern, not a complete view: it assumes the Blog model and its database are configured. If an image, date, related object, or other non-primitive field is needed, convert it deliberately to a string, number, boolean, list, or dictionary that represents the API contract. For larger or more complex serialization needs, Django’s serializers are another option; see the Django serialization documentation.

Build JSON manually with HttpResponse

Use HttpResponse when you need to control the serialization step yourself. Import both json and HttpResponse, encode a JSON-compatible Python value with json.dumps, and set content_type to application/json:

import json

from django.http import HttpResponse

return HttpResponse(json.dumps({"name": "Ada"}), content_type="application/json")

This lower-level path produces a JSON response when the supplied value can be encoded. For ordinary dictionaries and lists, JsonResponse is usually clearer because it handles JSON encoding and the content type for you. As with JsonResponse, convert model instances and QuerySet results into an explicit JSON-compatible structure first.