I'm new to Python and I'm trying to turn multiple csv files extracted form a relational database into a Json doc.
I've found a code that turns a single csv into a json doc but I want to maintain the relationships in the csv files and structure my Json doc according to that
import csv
import json
input_file = 'Test3.csv'
output_file_template = 'Test.json'
with open(input_file, 'r', encoding='utf8') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
rows = list(reader)
for i in range(len(rows)):
out = json.dumps(rows[1*i:1*(i+1)])
with open(output_file_template.format(i), 'w') as f:
f.write(out)
CSV1 - Student details
Studentid, Studentname, dob
001, Mark Johns, 21/04/2010
002, Jenny King, 20/01/2009
003, Steve Brown, 02/09/2010
CSV2- modules
Moduleid, Modulename
01, Maths
02, Science
03, Art
04, Geometry
CSV3 - class details
classid, classname
01, Goldfish room
02, Hawkins room
CSV4- timetable details
moduleid1, classid, studentid
01, 01, 003
01, 01, 002
02, 02, 001
I want to write into json document in the structure below:
"timetable details"
"header":{ "moduleid":, "locationid" },
"body": { "moduleid",
"Modules":{
"moduleid":,
"modulename":
},
"classid":,
"class details":{
"classid":,
"classname":
},
"studentid",
"studentdetails":{
"studentid":,
"studnetname":,
"dob":
}
}