Python – parsing dict object into variables to record to database [closed]

I am trying to learn python data types and I’m stuck at dict parsing. I have a dict object as below

{'0': {'Group Name': 'e', 'Inputs': {'0': {'DataName': 'fasd', 'DataType': 'data2'}}}, '1': {'Group Name': 's', 'Inputs': {'0': {'DataName': 'd', 'DataType': 'data1'}}}, '2': {'Group Name': 'g', 'Inputs': {'0': {'DataName': 'h', 'DataType': 'data1'}}}}

but I couldn’t parse it for group name, dataname and datatype.

I tried dictObj[iteratior]['Group Name'] but I saw we cannot use numerical index in dict objects. So I tried dictObj['Group Name'] but it didn’t work. What should be the proper approach?

I also tried it converting other object types but it also didn’t work.

dictform comes from frontend as json as the structure above. icformkey is the db queryset to be recorded in it.

for group in dictForm:

    if icformkey[iteration].level == 0:
        SingleQuery = icformkey[iteration]
        SingleQuery.name = group[str(iteration)]['Group Name']
        print("grup name", group[str(iteration)]['Group Name'])
        SingleQuery.full_clean()
        SingleQuery.save()
        print("group",  group)

    for input in group[str(iteration)]['Inputs']:
        if icformkey[iteration].level == 1:
            SingleQuery = icformkey[iteration]
            print("single query",SingleQuery)
            SingleQuery.name = input['DataName']
            SingleQuery.datatype = input['DataType']
            SingleQuery.full_clean()
            SingleQuery.save()
      
iteration+=1
output = {'result': True, 'error': 0}

return JsonResponse(output, safe=False)

Answer

If you notice, all the main keys of your dictionary are string representation of numbers: '1','2','0'. So, you need to specify quotes around the string to tell python that it is a string.

Since the values are nested dictionaries, you can go on referencing the keys to get the values

>>> dictObj={'0': {'Group Name': 'e', 'Inputs': {'0': {'DataName': 'fasd', 'DataType': 'data2'}}}, '1': {'Group Name': 's', 'Inputs': {'0': {'DataName': 'd', 'DataType': 'data1'}}}, '2': {'Group Name': 'g', 'Inputs': {'0': {'DataName': 'h', 'DataType': 'data1'}}}}
>>> dictObj['1']['Inputs']['0']['DataName']
'd'
>>> dictObj['1']['Inputs']['0']['DataType']
'data1'
>>> dictObj['1']['Group Name']
's'