Getting a DeserializationError when trying to load data from JSON to DB

I have this JSON file called data.json which is saved in my /fixtures folder. I cannot load my data into a model as I enter python manage.py loaddata data.json, I get this error.

I have made sure the file is in the proper JSON format but I can’t figure out what is causing this error.

Traceback (most recent call last):
File "/home/msd/.local/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in    Deserializer
yield from PythonDeserializer(objects, **options)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/serializers/python.py", line 91, in Deserializer
Model = _get_model(d["model"])
KeyError: 'model'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in    execute_from_command_line
utility.execute()
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
self.load_label(fixture_label)
File "/home/msd/.local/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 172, in load_label
for obj in objects:
File "/home/msd/.local/lib/python3.6/site-packages/django/core/serializers/json.py", line 73, in Deserializer
raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/msd/Projects/Django/markets/fixtures/data.json':

Your file is not in the proper format to be loaded as a JSON fixture within Django using loaddata.

See the sample for providing data with fixtures.

Hi Ken,

Specifically, for a JSON fixture to be loaded does my file require the model , pk and field keys?

Thanks

Generally speaking, yes. There’s a link in that page that points you to the JSON Serialization format, which provides a more formal and complete specification.

(The most common exception that I deal with is that the requirements are slightly different if you are using Natural Keys.)

2 Likes

hi KenWhitesell, I used the dumpdata argument of manage.py to create the JSON file I didn’t create it manually so but still, I am getting this error. Does that mean the dumpdata will not create the file as required for loading the fixtures and I will have to create it manually?

No, dumpdata works perfectly for creating files usable by loaddata.

There are a couple different reasons why it might fail - the most common reasons I’ve seen are either foreign key relationships not being properly maintained or natural keys not being used (or not being used correctly).

For example, if you’ve got Model X that has a ForeignKey field to Model Y. Then Model Y must be loaded before Model X.

If you’re having a specific issue loading a file, it might be helpful if you post the complete traceback message you’re getting, the model you’re trying to load, and perhaps the first two or three rows being loaded from the JSON file.