How to convert csv to json in python?

Dump after processing whole rows.


import csv
import json

with open('test.csv') as f:
    reader = csv.DictReader(f)
    rows = list(reader)

with open('test.json', 'w') as f:
    json.dump(rows, f)

For those who like one-liners:

import csv
import json

json_data = [json.dumps(d) for d in csv.DictReader(open('file.csv'))]

Checkout this fiddle for a working example: https://pyfiddle.io/fiddle/5992b8f4-552f-4970-91b6-a52cdee16ebc/?i=true