

Using pandas to do the heavy lifting, and assuming this valid csv file, this is one way of doing what you want: import jsonįor key, grp in df. Your data, converted to valid csv is saved in data.csv: PrimaryId,FirstName,LastName,City,CarName,DogName The CSV is structured as follows: PrimaryId,FirstName,LastName,City,CarName,DogNameīoth this post and this one have helped but I'm yet to create the correct structure. The CSV is generated from SQL which creates multiple rows for each primary id. For the test I made 100.000 lines in a csv file with copy/paste, and the whole conversion takes about half a second with Apple’s M1 Chip while the presented example took only 0.0005 seconds.I’m trying to convert a flat structured CSV into a nested JSON structure.You may write the JSON String to a JSON file.Convert the Python List to JSON String using json.dumps().Add the dictionary to the Python List created in step 1. Read the lines of CSV file using csv.DictReader() function.

To convert CSV to JSON in Python, follow these steps: Since I have more than 2000 rows, it is not a feasible solution.Ĭan anyone suggest how to parse CSV file line by line and convert it into JSON without specifying fieldnames? Answer by Milovan Tomašević Python CSV to JSON DataFrame.tojson(pathorbufNone, orientNone, dateformatNone, doubleprecision10, forceasciiTrue, dateunit'ms', defaulthandlerNone, linesFalse, compression'infer', indexTrue, indentNone, storageoptionsNone) source Convert the object to a JSON string. The csv module allows you to read a CSV file or write CSV data to a file. We don’t need to install them separately. Both these modules are incorporated with Python’s standard library. write ( ' \n ' )īut the problem with above code is that we need to mention the field names to parse the CSV. We are going to use csv and json libraries in this approach. DictReader ( csvfile, fieldnames ) for row in reader : json. Import csv import json csvfile = open ( 'file.csv', 'r' ) jsonfile = open ( 'file.json', 'w' ) fieldnames = ( "FirstName", "LastName", "IDNumber", "Message" ) reader = csv.
