0

I have a csv with my data that I turn into a JSON and then provide it as initial data for my Django Model.

Whenever I run

python manage.py migrate

I get

raise ValueError(errmsg("Extra data", s, end, len(s))) django.core.serializers.base.DeserializationError: Problem installing fixture 'PATH TO FOLDER/initial_data.json': Extra data: line 2 column 1 - line 666 column 643 (char 729 - 498863)

Line 666 column 643 is the last character in my JSON. Also even if cut the JSON in half it will still say the error is on the last character.

My code to turn the CSV into a JSON is:

import csv
import json

csvfile = open('organizationTest1.csv', 'rU')
jsonfile = open('initial_data.json', 'w')

fieldnames = ("role","name")
reader = csv.DictReader( csvfile, fieldnames)
a = 0
for row in reader:
    a += 1
    json.dump(row, jsonfile)
    jsonfile.write('\n')

If anyone knows why this is happening or how I could fix it I would love to know. Also if you have any questions I would be happy to answer them!

Soroush Ghodsi
  • 380
  • 1
  • 7
  • 19
  • Not sure if it's related but you should close the file (e.g. using`close()` function) once you're "done" with file handling operations. More regarding this topic on this [question](http://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important). – jlnabais Sep 14 '15 at 23:07
  • 1
    Will look into the question you linked but I added close() and I still get the same error. – Soroush Ghodsi Sep 14 '15 at 23:17
  • If you look at the docs on [initial data](https://docs.djangoproject.com/en/1.8/howto/initial-data/#providing-initial-data-with-fixtures) you can see that the fixtures contain information about which model it is. You don't have that, you're just dumping a dictionaries with two keys. – Alasdair Sep 14 '15 at 23:28

1 Answers1

0

You can't create a JSON file by sequentially dumping JSON dicts. You need to have a surrounding container - ie [] - and commas separating the rows.

The much easier way to do this would be to dump the whole data at once, rather than line by line: the json library will take care of the rest.

reader = csv.DictReader( csvfile, fieldnames)
json.dump(list(reader), jsonfile)

That will fix your actual error, but as Alasdair points out, this still won't work because initial_data is expected to be in a certain format.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895