So I have a CSV file that I convert later on to a JSON file. However what I want to do is that I want to save to a new json file for every json objects that is new. Meaning something like this:
{
"first_name": "Hello",
"last_name": "World",
"color": "black"
},
{
"first_name": "Stack",
"last_name": "Overflow",
"color": "Red"
}
How I change it to a format from CSV to JSON is that I create it as a dict where I have the CSV format and a fieldsname that is created based on "first_name", "last_name_" and color.
It would look like something like:
jsonfile = open('newfilejson.json', 'w')
fieldnames = ("first_name","last_name","color")
reader = csv.DictReader(csvfile, fieldnames)
for row in reader:
jsonfile.write('"first_name": "' + row['first_name'] + '",\n')
jsonfile.write('"last_name": "' + row['last_name'] + '",\n')
jsonfile.write('"color": "' + row['color'] + '",\n')
however this will just save into one file basically and my question is:
How can I make so everytime it finished one "row" from the for-loop to create a new json file that contains whats inside the for-loop (With write) and then whenever there is new, it creates a new json file. Basically meaning that everytime a row is finished, create new json?
for-loop around most code (except the two lines for opening the CSV-file) in which you open, write and close a new JSON-file on each iteration.