Converting a flat csv to nested json that has multi-level hierarchy using pandas


I am relatively new to python. However, I am asked to create a script that reads csv file and composes json request. I have explored couple of solutions available and created a script (pasted below) for converting to json. However, as I am unaware of ‘agg’ and ‘lambda’ functionalities, I am getting a json file that has single-level hierarchy. I am looking for multi-level and I am unsure of suitable methods to use.

import pandas as pd
import csv
import json

csvfile = ‘Responsibility_calculation_testdata.csv’
jsonfile = ‘Responsibility_calculation_json.json’

resp_calc_service = {
column_names = [‘member_person_id’,‘eligibility_id’,‘policy_id’,‘’,‘Claim.benefits_assignment_certification’,‘Claim.lines.line_number’,‘Claim.lines.amount_charged.int_val’]
df = pd.DataFrame(resp_calc_service)
df.to_csv(csvfile, header=column_names)

dff = pd.read_csv(csvfile)
dff = dff.groupby([“member_person_id”, “eligibility_id”, “policy_id”, “”, “Claim.benefits_assignment_certification”, “Claim.lines.line_number”, “Claim.lines.amount_charged.int_val”], as_index=False).agg(lambda x: list(x))
dff.to_json(jsonfile, orient=‘records’)

with open(jsonfile) as jsonfobj:
data_json = json.load(jsonfobj)

def unflatten_dic(dic):
for k,v in list(dic.items()):
subkeys = k.split(‘.’)
if len(subkeys) > 1:
dic[subkeys[0]].update({“”.join(subkeys[1:]): v})

def merge_lists(dic):
for k,v in list(dic.items()):
if isinstance(v, dict):
keys = list(v.keys())
vals = list(v.values())
if all(isinstance(l, list) and len(l)==len(vals[0]) for l in vals):
dic[k] =
val_tuple = set(zip(*vals)) # removing duplicates with set()
for t in val_tuple:
dic[k].append({subkey: t[i] for i, subkey in enumerate(keys)})
elif isinstance(v, list):
dic[k] = list(set(v)) # removing list duplicates

for user in data_json:

print(json.dumps(data_json, indent=4))

I am looking for build a request json that has a structure like this —
“member_person_id”: “3427424e958d4cccb7223c41cdb3c175”,
“eligibility_id”: “1d14c9a8139f4ff4b91c8f0b652b27e6”,
“policy_id”: “b1fac0c9-8160-421a-b6a6-c348bcf7b016”,
“ignored_claim_ids”: [“1SGT65T4”],
“claim”: {
“id”: “HALIME_TEST”,
“benefits_assignment_certification”: “YES”,
“lines”: [
“line_number”: 1,
“amount_charged”: {“int_val”: 143},
“allowed_amount”: {“int_val”: 500},
“benefit_id”: “vis_off_pc”,
“service_date_range”: {
“start”: {“year”: 2022, “month”: 3, “day”: 23},
“end”: {“year”: 2022, “month”: 3, “day”: 23}
Looking for an immediate help