I have a JSON file that contains multiple dictionary items based on username. Each username contains keys with values that say if VPN is enabled and where the admin boundaries are set. Example with two different dictionary items:
"jwick": {
"VPN1": {
"username": "jwick",
"2FA_STATUS": "2FA Registered and Active",
"ad_account_status": "AD Account Enabled",
"ADMIN_BOUNDARY": "United States"
},
"VPN2": {
"username": "jwick",
"2FA_STATUS": "2FA Registered and Active",
"ad_account_status": "AD Account Enabled",
"role_rdp_hosts": {
"[email protected]": {
"computerhostname": "ipaddress"
}
},
"ADMIN_BOUNDARY": "United States"
}
},
"jbond": {
"VPN1": {
"username": "jbond",
"2FA_STATUS": "2FA Registered and Active",
"ad_account_status": "AD Account Enabled",
"ADMIN_BOUNDARY": "England"
},
"VPN2": {
"username": "jbond",
"2FA_STATUS": "2FA Registered and Active",
"ad_account_status": "AD Account Enabled",
"role_rdp_hosts": {
"[email protected]": {
"hostname": "ipaddress"
}
},
"ADMIN_BOUNDARY": "England"
}
},
"JIJOE": {
"VPN1": {
"username": "JIJOE",
"2FA_STATUS": "2FA Locked",
"attempted_login": "358 days, 20 hours, 3 min",
"last_login": "489 days, 18 hours, 12 min",
"created": null,
"ad_account_status": "AD Account Enabled",
"ADMIN_BOUNDARY": "United States"
},
"VPN2": {
"username": "JIJOE",
"2FA_STATUS": "2FA Locked",
"ad_account_status": "AD Account Enabled",
"role_rdp_hosts": {
"[email protected]": {
"computername": "ipaddress"
}
},
"ADMIN_BOUNDARY": "United States"
}
}
I wanted to know if there was a way to print out the ADMIN_BOUNDARY
only for users with values of United States, even though every item in the JSON file is wrapped by a username. There are about 5000 usernames listed and no way for me to sort them because all of the usernames aren't wrapped into a dictionary. I just want to be pointed in the right direction on how to handle this.
I found someone else's post about checking for a value within the json file, but this required the top level dictionary item to be the same for all entries.
import json
check_boundary = United States
with open('vpnuserlist.json', 'r') as file:
data = json.load(file)
if check in data:
print(true)
CodePudding user response:
Loop over the dictionary entries, and test if any of the VPNs have the desired admin boundary.
for username, vpndict in data.items():
if any(vpn['ADMIN_BOUNDARY'] == "United States" for vpn in vpndict.values()):
print(username)
CodePudding user response:
Convert to dict and iterate over the users:
import json
check_boundary = 'United States'
us_users = []
with open('vpnuserlist.json', 'r') as file:
data = json.load(file)
for user, vpn_dict in data.items():
if any(vpn['ADMIN_BOUNDARY'] == check_boundary for vpn in vpndict.values()):
# Here you do whatever you want like append to a list or something
us_users.append(user)
print(user)