Constructing a Django API from a JSON File
By: Elliott Stein
Tags: Database, Django, Json, Fixtures, Data

D.R.E.A.M: Data Rules Everything Around Me. Get the data, analytics, analytics y’all

The more data an application successfully incorporates (and makes good use of) the better. Of course, someone could pile in terabytes of data to something and call it a day, but if it’s not used effectively, the data is almost useless. This writing intends to cover the first step of that process — incorporating data into your app. Many developers incorporate data using HTTP requests directly from their apps, but what if those requests aren’t readily available to the developer? Or you’re concerned the API may be deprecated one day. What if your data comes only in an Excel or CSV format and is not currently accessible over the web? What if the developer wants to internally structure the API in a different format than its original source? Seeding a database from a fixture file could potentially solve all of these problems.

This article assumes you have some basic Django Rest Framework background before starting this project. If you’ve never built a Django app, I suggest reading Parts I & II of How to Scaffold a Django API:

For my app and project, I am using the Trail API via Rapid API for a datasource. I pulled from the API by using the free subscription option, which allows you to make 500 fetch calls / month for free. I changed some of the header information (distance, number of calls, etc.), and copied the code snippet on the right panel to a JS file. I removed any error catching, and changed to two .then statements: 1) the first parsing the response and; 2) a console.log. I opened the JS file via lite-server, and ran the fetch call from the code snippet. After waiting 1–2 seconds, you should see a response come through your console. To copy this response, right click and chose: Store as global variable. Then, type: copy(temp1) in your console. Open a blank .json file and you should be able to paste in everything from the fetch all.

Rapid API UI Interface

Now that we have JSON data, let’s put it into our Django app. Your class model fields have to match identical to the JSON file, or you’ll be tossed an error. Also, you will likely use what is already stored in this classes’ database.

After your field names on your model match in identical fashion to what is stored in the JSON file, run:

python manage.py makemigrations

and

python manage.py migrate

Within your app’s directory folder, create a new folder titled:

fixtures

Within that fixtures folder, move the JSON file you wish to incorporate into your project. Title the file the name of the Class you wish to incorporate (so for my Hiking App, which has a Trails class, my file was called):

src/django_app/fixtures/trail.json

The JSON file should start with the following syntax:

[{ "model": "django_app.home", "pk": 1, "fields": { "name": "dummy data", "age": "24", ... }

}]

The model value should match the name of your JSON file (and also, the name of the Class you are trying to incorporate). Each record in the data should start with a key “pk” with values incrementing by 1 each record, and then, a fields key with an object/hash set as its value.

Repeat: the Fields key names in the JSON file must be identical to the Fields within your model’s class.

After everything is setup correctly, you should be able to run:

python manage.py loaddata <name of file>

The first couple of times I tried this I had to keep working my JSON file to fit perfectly with my class field names. Django gives really precise and helpful errors though, so you can change your fixture file easily. Here is how your terminal should look after running the load data command:

Your database should be seeded (or loaded) up! Hope you can incorporate some good data into your project, happy coding!