How to Import a JSON File to a Django Model?
Last Updated :
24 Apr, 2025
In many web development projects, it's common to encounter the need to import data from external sources into a Django application's database. This could be data obtained from APIs, CSV files, or in this case, JSON files. JSON (JavaScript Object Notation) is a popular format for structuring data, and integrating it into a Django project can be quite beneficial.
Importing JSON Data into Models in Django
In this project, we'll explore how to import JSON data into a Django database using a Python script. We'll create a Django application, define a model to represent our data, write a Python code to parse the JSON file and save it into the database, and execute the code to import the data.
Starting the Project Folder
To start the project use this command:
django-admin startproject json_import_project
cd json_import_project
To start the app use this command
python manage.py startapp book
Now add this app to the ‘settings.py’
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"book",
]
json file (script.json)
[
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"publication_year": 1960
},
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"publication_year": 1925
}
]
File Structure

Importing JSON files into Django models is a useful skill. To master this along with other complex Django tasks, the Complete Django Web Development Course will guide you through.
Setting Necessary Files
models.py :Below code defines a Django model named "Book" with three fields: "title" (a character field with a maximum length of 100 characters), "author" (also a character field), and "publication_year" (an integer field).
Python
# In book/models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
publication_year = models.IntegerField()
views.py :Below, code defines a view function named "import_data" that handles POST requests. It checks if a JSON file is uploaded, reads its content, iterates over each item in the data, creates a new Book object for each item, and saves it to the database.
Python
# In book/views.py
from django.shortcuts import render
from .models import Book
import json
def import_data(request):
if request.method == 'POST' and request.FILES['json_file']:
json_file = request.FILES['json_file']
data = json.load(json_file)
for item in data:
book = Book(
title=item['title'],
author=item['author'],
publication_year=item['publication_year']
)
book.save()
return render(request, 'success.html')
return render(request, 'form.html')
Creating GUI
form.html: Below, HTML code defines a form page titled "Import JSON Data". It contains a form with a file input field where users can upload a JSON file. Upon submission, the form sends a POST request with the uploaded file to the server for processing.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import JSON Data</title>
</head>
<body>
<h1>Import JSON Data</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="json_file">
<button type="submit">Import</button>
</form>
</body>
</html>
success.html : HTML code defines a page titled "Import Successful". It notifies users that the JSON data has been successfully imported into the database. Additionally, it provides a link for users to navigate back to the import page.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import Successful</title>
</head>
<body>
<h1>Import Successful!</h1>
<p>The JSON data has been successfully imported into the database.</p>
<a href="/">Go back to import page</a>
</body>
</html>
urls.py: Below, is the urls.py file connect HTML with views.py file.
Python
from django.contrib import admin
from django.urls import path
from book.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('', import_data),
]
admin.py: Here, we registered our model.
Python
from django.contrib import admin
from book.models import *
# Register your models here.
admin.site.register(Book)
Deployment of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output

Video Demonstration
Similar Reads
How to Get a List of the Fields in a Django Model
When working with Django models, it's often necessary to access the fields of a model dynamically. For instance, we might want to loop through the fields or display them in a form without manually specifying each one. Django provides a simple way to get this information using model meta options. Eac
2 min read
How to Extract and Import file in Django
In Django, extracting and importing files is a common requirement for various applications. Whether it's handling user-uploaded files, importing data from external sources, or processing files within your application, Django provides robust tools and libraries to facilitate these tasks. This article
4 min read
How to Convert Models Data into JSON in Django ?
Django is a high-level Python based Web Framework that allows rapid development and clean, pragmatic design. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database SQLlite3, etc. How to Convert Models
2 min read
How to get JSON data from request in Django?
Handling incoming JSON data in Django is a common task when building web applications. Whether you're developing an API or a web service, it's crucial to understand how to parse and use JSON data sent in requests. In this article, we will create a simple Django project to demonstrate how to retrieve
2 min read
How to import JSON File in MongoDB using Python?
Prerequisites: MongoDB and Python, Working With JSON Data in Python MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs. JSON stands for JavaScript Object Notation
2 min read
How to add RSS Feed and Sitemap to Django project?
This article is in continuation of Blog CMS Project in Django. Check this out here - Building Blog CMS (Content Management System) with Django RSS (Really Simple Syndication) Feed RSS (Really Simple Syndication) is a web feed that allows users and applications to access updates to websites in a stan
3 min read
How to Add Data in JSON File using Node.js ?
JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article. Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
4 min read
Django - How to Create a File and Save It to a Model's FileField?
Django is a very powerful web framework; the biggest part of its simplification for building web applications is its built-in feature of handling file uploads with FileField. Images, documents, or any other file types, Django's FileField makes uploading files through our models easy. In this article
5 min read
How to Add a Custom Field in ModelSerializer in Django
A key component of Django Rest Framework (DRF) is the Serializer, which converts complex data types like Django models into JSON, XML, or other content types. The ModelSerializer, a subclass of Serializer, automatically creates fields based on the modelâs fields, significantly reducing boilerplate c
4 min read
How to Filter ForeignKey Choices in a Django ModelForm
A ForeignKey field allows one model to relate to another in Django. When this field is represented in a form (e.g., a Django ModelForm), it typically displays a dropdown list (a select box) populated with all the objects from the related model. However, there are many scenarios where we may need to
7 min read