I'm trying to parse specific values from a dictionary. That dictionary is a json payload from an API query. I've tried numerous ways but I'm just not able to get exactly what I want.
My goal is to:
- Parse through a list of existing names. I have a blank dictionary that imports from a file.
- See if that name is active per the API response.
- print a list of active users: If user is active print ("User: %s is Active" % user)
- Then print a list of inactive users: else print ("User: %s is not Active" % user)
To get the values I have:
#!/usr/bin/env python
import requests
from requests.auth import HTTPBasicAuth
import json
import sys
import getpass
password = getpass.getpass()
url = "https://myurl/blah/blah/getusers=true"
request = requests.get(url,auth=HTTPBasicAuth('username', password.format(password)), verify=True)
if(request.ok):
data = json.loads(request.content)
for key in data:
users = data[key]
print users
else:
request.raise_for_status()
What is being returned:
https://myurl/blah/blah/getusers=true
getusers
users{u'max-results': 50, u'items': [{u'displayName': u'testfirstname, testlastname', u'name': u'testfirstname', u'self': u'https://blah/blah', u'emailAddress': u'[email protected]', u'key': u'myusername', u'active': True, u'timeZone': u'America/Los_Angeles'},
I don't if it's just that I'm having trouble finding which key to get or what. The key in the response is: u'active': True. I've tried to pull the 'active' value but to no avail. Any help would be appreciated.
edit: more info. So, the payload returns a list of users with data for each user. The "active: True" is just one parameter that defines the user in the system. I want to take a list of users I already have and check the API to see if each user in my list is either "active: True" or "active: False". I'm not sure why the first two lines are returned (the url and getusers)
json.loads()will give you a nested dictionary. Inspect the keys at multiple levels to find what you want. I'd need a more specific question to say more - in fact I don't really know what you are looking for, or what your data looks like.printand I'm not sure what we're looking at there.