convert csv file to json object python code example

Example 1: python csv string to json

import json
import csv 
//string_data is your string in csv format
json.dumps(list(csv.DictReader(string_data.splitlines(), delimiter=",")))

Example 2: python json from csv

import csv
import json

csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')

fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')