I am working on an export of user info to .csv, and have a GET which returns user data in the form
{
"id": "6584381e-34c3-4530-80f7-5fb06b1e88a2",
"name": "Alan Hege",
"chat": {
"jabberId": "53625176d210c22a6f588754@inineducation.orgspan.com"
},
"department": "Administration",
"email": "alan.hege@inineducation.com",
"primaryContactInfo": [
{
"display": "8002",
"mediaType": "PHONE",
"type": "PRIMARY"
},
{
"address": "alan.hege@inineducation.com",
"mediaType": "EMAIL",
"type": "PRIMARY"
}
],
"addresses": [
{
"address": "+13179838002",
"display": "+1 317-983-8002",
"mediaType": "PHONE",
"type": "WORK"
},
{
"display": "8002",
"mediaType": "PHONE",
"type": "WORK2"
}
]
(Plus a bunch more)
Part of my Python attempts to write specific values out to a file using writelines()
I am trying to figure out how to conditionally use the "address" since some users do not have an assigned DID, so the write fails.
Something along the lines of (following bad syntax):
file.writelines([user['name'] + ',' + user['email'] + ',' if len(user["addresses"][0]["address"]) > 0: + user["addresses"][0]["address"] + '\n' for user in usersList])
The syntax for the ternary conditional operator in python is a bit different than most languages. It follows the format of <true value> if <condition> else <false value>
. So you might use this expression:
user["addresses"][0]["address"] if hasattr(user["addresses"][0], "address") else "no address"
to write out the address or no address
if the address property doesn't exist.
Hmmm...well, that wrote out "no address" for all of the users, even though all but one have an address populated.
Does user["addresses"][0] function as an object?
Just doing a simple GET on one user, trying to get one value from the result, I have
parameter = getattr(res,"email")
print parameter
Which gets me the error
parameter = getattr(res,"email")
AttributeError: 'dict' object has no attribute 'email'
So, I'm not sure the user object is being viewed as such?
Are you using the Python SDK? I wrote a test script for this and it works fine:
import PureCloudPlatformApiSdk
from PureCloudPlatformApiSdk.rest import ApiException
PureCloudPlatformApiSdk.configuration.access_token = 'mytoken'
try:
api_instance = PureCloudPlatformApiSdk.UsersApi()
user_list = api_instance.get_users()
for user in user_list.entities:
print "%s lives at %s" % (user.name, user.addresses[0].display if len(user.addresses) > 0 else "no addresses")
except ApiException as e:
print "Exception when calling get_users: %s\n" % e
print 'done'
and it generated the output:
BoJack Horseman lives at no addresses
Generic lives at no addresses
Hollywoo Admin lives at 1000
Princess Carolyn lives at no addresses
Vincent Adultman lives at no addresses
done
No, was going a different route based on a sample someone else gave me. It was older, and I really should have started with the SDK.
Ok, I don't know what types you're working with then. You can use the statement print type(var_name)
to print out what type var_name
is and then use appropriate syntax for those types.