'Having an issue parsing through this json in python

I have created a var that is equal to t.json. The JSON file is a follows:

{
    "groups": {
        "customerduy": {
            "nonprod": {
                "name": "customerduynonprod",
                "id": "529646781943",
                "owner": "[email protected]",
                "manager_email": ""
            },
            "prod": {
                "name": "phishing_duyaccountprod",
                "id": "241683454720",
                "owner": "[email protected]",
                "manager_email": ""
            }
        },
        "customerduyprod": {
            "nonprod": {
                "name": "phishing_duyaccountnonprod",
                "id": "638968214142",
                "owner": "[email protected]",
                "manager_email": ""
            }
        },
        "ciasuppliergenius": {
            "prod": {
                "name": "ciasuppliergeniusprod",
                "id": "220753788760",
                "owner": "[email protected]",
                "manager_email": "[email protected]"
            }
        }
    }
}

my goal was to pars this JSON file and get value for "owner" and output it to a new var. Example below:

t.json = group_map
group_id_aws = group(
            group.upper(), 
            "accounts", 
            template, 
            owner = group_map['groups']['prod'], 
            manager_description = "Groups for teams to access their product accounts.", 

The error I keep getting is: KeyError: 'prod'



Solution 1:[1]

Owner occurs 4 times, so here is how to get all of them.


import json

# read the json
with open("C:\\test\\test.json") as f:
    data = json.load(f)

# get all 4 occurances
owner_1 = data['groups']['customerduy']['nonprod']['owner']
owner_2 = data['groups']['customerduy']['prod']['owner']
owner_3 = data['groups']['customerduyprod']['nonprod']['owner']
owner_4 = data['groups']['ciasuppliergenius']['prod']['owner']

# print results
print(owner_1)
print(owner_2)
print(owner_3)
print(owner_4)


the result:

[email protected]
[email protected]
[email protected]
[email protected]

Solution 2:[2]

You get a key error since the key 'prod' is not in 'groups' What you have is

group_map['groups']['customerduy']['prod']
group_map['groups']['ciasuppliergenius']['prod']

So you will have to extract the 'owner' from each element in the tree:

def s(d,t):
    for k,v in d.items():
        if t == k:
            yield v
        try:
            for i in s(v,t):
                yield i
        except:
            pass

print(','.join(s(j,'owner')))

Solution 3:[3]

If your JSON is loaded in variable data, you can use a recursive function that deals with the two containers types (dict and list) that can occur in a JSON file, recursively:

def find_all_values_for_key(d, key, result):
    if isinstance(d, dict):
        if key in d:
            result.append(d[key])
            return
        for k, v in d.items():
            find_all_values_for_key(v, key, result)
    elif isinstance(d, list):
        for elem in d:
            find_all_values_for_key(elem, key, result)


owners = []
find_all_values_for_key(data, 'owner', owners)

print(f'{owners=}')

which gives:

owners=['[email protected]', '[email protected]', '[email protected]', '[email protected]']

This way you don't have to bother with the names of intermediate keys, or in general the structure of your JSON file.

You don't have any lists in your example, but it is trivial to recurse through them to any dict with an owner key that might "lurk" somewhere nested under a a list element, so it is better to deal with potential future changes to the JSON.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Anthon