Django JsonResponse
last modified November 13, 2026 Python 3.15
Django JsonResponse tutorial shows how to send JSON data with JsonResponse in Django. The examples use Django 6.1 and Python 3.15, and the environment is managed with uv.
Django
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It is maintained by the Django Software Foundation. If you are new to the framework, start with the Django introduction article.
JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. The official Internet media type for JSON is
application/json. The JSON filename extension is
.json. It is easy for humans to read and write and for machines to
parse and generate.
Django JsonResponse
JsonResponse is an HttpResponse subclass that helps
to create a JSON-encoded response. Its default Content-Type header
is set to application/json. The first parameter,
data, should be a dict instance. If the
safe parameter is set to False, any JSON-serializable
object can be passed; otherwise only dict instances are allowed.
Creating the project
In the following example, we create a Django application that sends JSON data to the client. It has two views: one returns a dictionary and the other a list of dictionaries.
$ mkdir jsonresponse $ cd jsonresponse $ uv init --bare $ uv add django $ uv run django-admin startproject config .
We create a project directory, initialize it with uv, and install Django into a
project-local virtual environment. The last command creates the Django project
inside the current directory; the dot at the end prevents Django from
creating an extra config directory level. All commands are run
with uv run, so there is no need to activate the environment.
Note: The Django way is to put functionality into apps, which
are created with uv run python manage.py startapp. In this
tutorial, we do not use an app to keep the example simple. We focus on sending
a JSON response.
Project structure
We add a views.py module to the config package.
The project then has the following structure; the hidden .venv
directory is not shown.
. ├── config │ ├── asgi.py │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ ├── views.py │ └── wsgi.py ├── manage.py ├── pyproject.toml └── uv.lock
URL configuration
The URL configuration maps request paths to view functions. Django reads the urlpatterns list from top to bottom and calls the first view whose path matches.
from django.contrib import admin
from django.urls import path
from .views import bad, user, users
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', users, name='users'),
path('user/', user, name='user'),
path('bad/', bad, name='bad'),
]
We add three routes. The users/ and user/ paths send
valid JSON; the bad/ path demonstrates the error we get when the
safe parameter is needed but not set. The module docstring
generated by Django is omitted from the listing.
The views
The views are plain Python functions. Each receives the HttpRequest and returns a response object.
from django.http import JsonResponse
def users(request):
data = [{'name': 'Peter', 'email': 'peter@example.org'},
{'name': 'Julia', 'email': 'julia@example.org'}]
return JsonResponse(data, safe=False)
def user(request):
data = {'name': 'Peter', 'email': 'peter@example.org'}
return JsonResponse(data)
def bad(request):
return JsonResponse([1, 2, 3])
The users view defines a list of dictionaries. Since it is a list,
we must set safe to False. The user view
sends a single dictionary, which needs no extra parameter. The bad
view passes a list without safe=False; JsonResponse
raises a TypeError, and Django returns the status code 500.
Note: The safe check exists for historical
reasons: very old browsers could be tricked into executing a top-level JSON
array as JavaScript. Modern browsers are not affected, so
safe=False is fine for sending lists.
Running the application
We start the development server.
$ uv run python manage.py runserver
In another terminal, we use the curl tool to make the GET
requests. The -i option prints the headers, which show the
application/json content type. The request to
/bad/ returns the status code 500, and the server log contains the
TypeError with the message that tells us to set the
safe parameter.
$ curl -i http://127.0.0.1:8000/users/
HTTP/1.1 200 OK
Content-Type: application/json
...
[{"name": "Peter", "email": "peter@example.org"}, {"name": "Julia", "email": "julia@example.org"}]
$ curl http://127.0.0.1:8000/user/
{"name": "Peter", "email": "peter@example.org"}
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/bad/
500
Source
Django documentation: JsonResponse objects
In this article we have sent JSON data from Django views with JsonResponse. The base class is described in Django HttpResponse, and files are sent with Django FileResponse.
Author
List all Django tutorials.