2
list1=[{'value': [{'children': [{'children': [{'children': [{'children': [{'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller', 'name': 'controller'}, {'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate', 'name': 'delegate'}, {'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java', 'metric': 'coupling', 'name': 'IncentiveRateJPARepositoryImpl.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl', 'name': 'impl'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src', 'name': 'src'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv', 'name': 'vvv'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz', 'name': 'xyz'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271', 'name': '89224495-b8b3-43c9-ac8d-addb755ab271'}]}]

required list is

        list2=[
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, 
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'},
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}, 
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java', 'metric': 'coupling', 'name': 'IncentiveRateJPARepositoryImpl.java'}]

above list is a column in pandas dataframe

begadali
  • 17
  • 4
  • Can you provide what you tried so far? Also, you should look at [how to ask](https://stackoverflow.com/help/how-to-ask) – Nonlinear Jan 04 '23 at 17:36
  • This should get you started: https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists – JonSG Jan 04 '23 at 18:29

1 Answers1

0

Create a recursive function that can go deeper to the dictionary that contains one level above key in your case 'coupling' is the required one , capture those dictionaries in a list while in recursion that list will your data :)

def deep(data, key,l):
    if isinstance(data, list):
        for d in data:
             deep(d, key,l)
    if isinstance(data, dict):
        if key in data.keys():
            l.append(data)
            #return data
        else:
            for dkey in data.keys():
                if isinstance(data.get(dkey), dict):
                     deep(data.get(dkey), key,l)
                if isinstance(data.get(dkey), list):
                    for d in data.get(dkey):
                         deep(d, key,l)
                else:
                    continue
    return l #you must return list
                    
l=[]
deep(list1,'coupling',l)
l
Out[161]: 
[{'coupling': {'category': 'A', 'value': '0'},
  'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java',
  'metric': 'coupling',
  'name': 'IncentiveRateController.java'},
 {'coupling': {'category': 'A', 'value': '0'},
  'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java',
  'metric': 'coupling',
  'name': 'MaintainIncentiveController.java'},
 {'coupling': {'category': 'A', 'value': '0'},
  'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java',
  'metric': 'coupling',
  'name': 'IncentiveRateDelegate.java'},
 {'coupling': {'category': 'A', 'value': '0'},
  'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java',
  'metric': 'coupling',
  'name': 'IncentiveRateJPARepositoryImpl.java'}]

To apply on df

l=[]
df = pd.DataFrame([list1],columns=['A'])
df['B']=df['A'].apply(lambda x: deep(x,'coupling',l))
df['B']
Out[133]: 
0    [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java',...
Name: B, dtype: object
Surjit Samra
  • 4,614
  • 1
  • 26
  • 36