1

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:

  1. Parse through a list of existing names. I have a blank dictionary that imports from a file.
  2. See if that name is active per the API response.
  3. print a list of active users: If user is active print ("User: %s is Active" % user)
  4. 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)

2
  • 1
    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. Commented Sep 21, 2017 at 18:17
  • The dictionary output in your printout, what actually is that? Is that the whole response? You have 3 lines of output but only one print and I'm not sure what we're looking at there. Commented Sep 21, 2017 at 18:23

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

This gets me what I want, thanks. I just need to figure out how to compare the two dictionairies. When i try to print the list with print active.items(), i get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.