skip to Main Content

I have JSON object something like below,

Data = [
    {
        'Name': 'A1',
        'Value': 10,
        'Type': 'AAA'
    },
    {
        'Name': 'A1',
        'Value': 20,
        'Type': 'AAA'
    },  
    {
        'Name': 'B1',
        'Value': 10,
        'Type': 'AAA'
    },
    {
        'Name': 'C1',
        'Value': 10,
        'Type': 'BBB'
    },
    {
        'Name': 'D1',
        'Value': 10,
        'Type': 'BBB'
    }
]

And i would like to split the object into a list based on "Type" and then based on the "Name" also to something like below,

Data = {
    'AAA': {
        'A1': [
            {
                'Name': 'A1',
                'Value': 10,
                'Type': 'AAA'
            },
            {
                'Name': 'A1',
                'Value': 20,
                'Type': 'AAA'
            },          
        ],
        'B1': [
            {
                'Name': 'B1',
                'Value': 10,
                'Type': 'AAA'
            },      
        ]
    },
    'BBB': {
        'C1': [
            {
                'Name': 'C1',
                'Value': 10,
                'Type': 'BBB'
            }   
        ],
        'D1': [
            {
                'Name': 'D1',
                'Value': 10,
                'Type': 'BBB'
            },      
        ]   
    }
}

To achieve this, so far i have tried looping over the whole data and then splitting them into separate objects based on the "Type" and then create a unique list of "Name", then iterating over the newly created objects to split them based on the Name.

I was doing something like this,

tTmp_List_1 = []
tTmp_List_2 = []

tTmp_Name_List_1 = []
tTmp_Name_List_2 = []

for tValue in Data:
    if (tValue['Type'] == 'AAA'):
        tTmp_List_1.append(tValue)
        tTmp_Name_List_1.append(tValue['Name'])

    if (tValue['Type'] == 'BBB'):
        tTmp_List_2.append(tValue)  
        tTmp_Name_List_2.append(tValue['Name'])          

tTmp_Name_List_1 = list(set(tTmp_Name_List_1))
tTmp_Name_List_2 = list(set(tTmp_Name_List_2))

From the above "tTmp_Name_List_1" and "tTmp_Name_List_2", i am about to iterate over the list and then match the matching names with the initial Data object to come up with separated list objects based on the names and then set it back with something like this

tTmp_Dict = {}
for tTmp_Name in tTmp_Name_List_1 :
    tTmp = []
    if (Data['Name'] == tTmp_Name):
        tTmp.append(Data)
    
    tTmp_Dict.append(tTmp)

Can someone kindly suggest me or help me with a more better way of doing this.

Any help is much appreciated.

Thanks,

2

Answers


  1. Try this.

    organized_data = {}
    for item in Data:
        item_type = item['Type']
        item_name = item['Name']
        
        if item_type not in organized_data:
            organized_data[item_type] = {}
        
        if item_name not in organized_data[item_type]:
            organized_data[item_type][item_name] = []
        
        organized_data[item_type][item_name].append(item)
    
    # Output the organized data
    print(organized_data)
    
    Login or Signup to reply.
  2. from collections import defaultdict
    data = 'your nested dict'
    
    output_dict = defaultdict(lambda: defaultdict(list))
    for element in data:
        output_dict[element['Type']][element['Name']].append(element)
        
    output = {k : dict(v) for k, v in output_dict.items()}
    print(output)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search