0

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":
        }
}
elmify
  • 105
  • 1
  • 5
  • 16

1 Answers1

0

Use CSV reader to parse CSV files into Python objects, from those objects create object that has desired format as in your example, then use JSON dump to create a JSON formatted string that you can write to file

Marek
  • 1,413
  • 2
  • 20
  • 36
  • thank you! I think the key thing here will be creating an object whilst retrieving the relevant fields from each file. Could you maybe direct me to a right resource for this? – elmify Jul 27 '18 at 12:26