Practice Problem..!

The following is an exercise for you folks.
for the given dictionary, save the details of each individual in json file with name of each file as a combination of the name and pan of the individual. do not mind the individual details. they are mock details.

dict1={1:{'name':'xyz', 'pan':123,'details':{1:4, 3:1}},
2:{'name':'abc', 'pan':345,'details':{1:5, 3:1}},
3:{'name':'pqr', 'pan':234,'details':{1:10, 3:0}}}

for example, name of the file for first user should look like xyz_123.json and contents of the json file should be {1:4, 3:1}
Let us know how you would approach the question and write down your answers. Hope you enjoy.

2 Likes
import json
for k,v in dict1.items():
    F_name=v['name']+'_'+str(v['pan'])+'.json'
    with open(F_name,'w') as outfile:
        json.dump(v['details'],outfile)
1 Like