I have a CSV file with a bunch of columns. A few of the columns are the same, but I want to convert them to a JSON object where they all live under the same array.
For example in the CSV:
firstname,lastname,pet,pet,pet
Joe, Dimaggio, dog, cat
Pete, Rose, turtle, cat
Jackie, Robinson, dog
I want the JSON to be
{ firstname: Joe,
lastname: Dimaggio,
pets: ["dog", "cat"]
},
{ firstname: Pete,
lastname: Rose,
pets: ["turtle", "cat"]
},
{ firstname: Jackie,
lastname: Robinson,
pets: ["dog"]
}
I'm trying to write a simple Python script to do this but I'm running into problems.
Here's what I've got so far:
import csv
import json
csvfile = open('userdata.csv', 'r')
jsonfile = open('userdata.json', 'w')
fieldnames = ("firstname", "lastname", "pet", "pet", "pet");
reader = csv.DictReader( csvfile, fieldnames)
record = {}
for row in reader:
record['firstname'] = row['firstname']
record['lastname'] = row['lastname']
record['pets'] = json.JSONEncoder().encode({"pets": [row['pet'], row['pet'], row['pet'], row['pet'], row['pet']]});
json.dump(record, jsonfile, indent=4)
##json.dump(json.loads(json.JSONEncoder(record)), jsonfile, indent=4)
print "something worked"
But this is acting funny since it's printing pets as an array inside an object called pets.
I can't figure out how to get the array pets outside the object `pets. Also it's adding backslashes to the array items
{
"firstname": "Joe",
"lastname": "Dimaggio",
"pets": "{\"pets\": [\"dog\", \"cat\"]}"
}