Thanks for your reply. The 'id' must exactly same with the header in csv. It's case sensitive.
I updated the content
date, ref_num, description,debit,credit,coa_code, coa_name
2018-12-28, PV18/12/066, Jacky , ,59.80,310-1000, Samboi
I updated the content
date, ref_num, description,debit,credit,coa_code, coa_name
2018-12-28, PV18/12/066, Jacky , ,59.80,310-1000, Samboi
import csv import json from itertools import groupby with open('testing.csv', 'r') as csv_ledger: r = csv.DictReader(csv_ledger) data = [dict(d) for d in r] groups = [] for k, g in groupby(data, lambda r: (r['ref_num'], r['date'])): groups.append({ "date": k[1], "ref_num": k[0], "items": [{k: v for k, v in d.items() if k not in ['ref_num', 'date']} for d in list(g)] }) print(json.dumps(groups[:10], indent=4))Output result
"date": "2018-12-28", "ref_num": "PV18/12/066", "items": [ { "description": "Jacky", "debit": "", "credit": "59.80", "coa_code": "310-1000", "coa_name": "Samboi" },Expected Result
"date": "2018-12-28", "ref_num": "PV18/12/066", "items": [ { "description": "SHARMINI SHANKAR", "debit": "", "credit": "59.80", "new_of_account": { "id": "1", "coa_code": "310-1000", "coa_name": "samboi" } },