I have a list of json objects like this -
[{Level1: "A", Level2: "B", Level3: "C", Level4: "item1"}, 
 {Level1: "A", Level2: "B", Level3: null, Level4: "item2"}, 
 {Level1: "D", Level2: null, Level3: null, Level4: "item3"}]
In Python, I want to group them by level to create a tree structure.
{text: "root": items: 
    [{text: "A", items: [ 
         {text: "B", items: [
              {text: "C", items: [
                  text: "item1", items:[]]}, 
              {text: "item2", items: []}}]}, 
     {text: "D", items: [{text: "item3", items: []}]}
    ]
]}
# pseudocode 
result = dict()
result["text"] = "root"
result["items"] = [] 
d = {"Level1": set(), "Level2": set(), "Level3": set(), "Level4": set()}
for row in data_rows: 
    insertLocation = result["items"] 
    for key in ["Level1", "Level2", "Level3", "Level4"]: 
        txt = row[key]
        if txt in d[key]: 
            for j in insertLocation: 
                if j.text = txt: 
                     insertLocation = j
                     break  
        else: 
            newItem = {"text": txt, "items": []}
            insertLocation = newItem.items
            d[key].add(txt)
        
Can anyone provide any feedback on my code to perhaps make it more efficient? (or if there's a better way to do this, would be super great to know). I'm really looking to maximize efficiency.
 
     
     
    