Basic Python csv to json script gets the column order mixed up in the final JSON. Any idea why?
test.csv
animal,age,count,legs
dogs,3,5,4
cats,6,4,4
birds,2,1,2
script
import csv
import json
csvfile = open('test.csv', 'r')
jsonfile = open('test.json', 'w')
reader = csv.DictReader( csvfile)
jsonfile.write('[')
for row in reader:
json.dump(row, jsonfile)
jsonfile.write(',\n')
jsonfile.write(']')
test.json
[{"count": "5", "age": "3", "legs": "4", "animal": "dogs"},
{"count": "4", "age": "6", "legs": "4", "animal": "cats"},
{"count": "1", "age": "2", "legs": "2", "animal": "birds"},
]