Reputation: 605
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:
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)
Upvotes: 1
Views: 343
Reputation: 780
Looks to me like everything you want is inside the items level of the json. Try something like this:
data = json.loads(request.content)
items = data['items']
active=[]
inactive=[]
for i in range(len(items)):
user = items[i]
if user['active']:
active.append(user)
else:
inactive.append(user)
Upvotes: 1