I have a CSV file which is converted into a JSON file with a Python script. It works but I want JSON to have the same sorting of CSV file. This is what I've tried without success:
#!/usr/bin/python
import csv
import json
CSV_PATH = './myFile.csv'
JSON_PATH = './myFile.json'
csv_file = csv.DictReader(open(CSV_PATH, 'r'))
json_list = []
for row in csv_file:
json_list.append(row)
sortedlist = sorted(json_list, key=lambda row:(row['id']), reverse=False)
file(JSON_PATH, 'w').write(json.dumps(
sortedlist, sort_keys=False, indent=2, separators=(',', ': '),
encoding="utf-8",ensure_ascii=False))
My CSV looks like that:
id,name,lastname
1,John,Red
2,Steve,Brown
But the created JSON looks like that even with that lambda function:
[
{
"name": "John",
"id": "1",
"lastname": "Red"
},
{
"name": "Steve",
"id": "2",
"lastname": "Brown"
}
]
What I'd like to get is "id, "name" and then "lastname".