Andrew Swift
Andrew Swift

Reputation: 2237

Error when using manage.py loaddata to load Django JSON file created with dumpdata

I am using ./manage.py loaddata file.json to load a JSON file created with ./manage.py dumpdata > ../directory/file.json, and I get the following error:

matching_chars: 6                                                               
Traceback (most recent call last):                                              
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/serializers/json.py", line 69, in Deserializer
    objects = json.loads(stream_or_string)                                      
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads                
    return _default_decoder.decode(s)                                           
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode                
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())                           
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode            
    raise JSONDecodeError("Expecting value", s, err.value) from None            
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)         
                                                                                
The above exception was the direct cause of the following exception:            
                                                                                
Traceback (most recent call last):                                              
  File "./manage.py", line 22, in <module>                                      
    main()                                                                      
  File "./manage.py", line 18, in main                                          
    execute_from_command_line(sys.argv)                                         
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()                                                           
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)                     
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)                                          
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)                                      
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 78, in handle
    self.loaddata(fixture_labels)                                               
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 123, in loaddata
    self.load_label(fixture_label)                                              
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 181, in load_label
    for obj in objects:                                                         
  File "/opt/venv/djangoEnv/lib/python3.8/site-packages/django/core/serializers/json.py", line 74, in Deserializer
    raise DeserializationError() from exc                                       
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/example/rep/file.json':

I don't understand how there can be an error — if the file is both created and read by the same program (manage.py), why doesn't it work?

Upvotes: 0

Views: 338

Answers (1)

Andrew Swift
Andrew Swift

Reputation: 2237

[update] running ./manage.py dumpdata --skip-checks > file.json also solves the problem.


I found the answer thanks to this Reddit post.

By using ./manage.py dumpdata --output ../directory/file.json, I got a JSON file that could be imported without any errors.

Then I used a vim diff to find the difference between the working (--output) file and the broken (>) file.

At the top of the broken JSON file, the first line was:

matching_chars: 6

I don't know why using dumpdata with output redirection caused this to be added, but removing it solved my problem.

Ironically, the matching_chars: 6 was literally the first line of the error message — I just hadn't understood it.

Upvotes: 0

Related Questions