I'm new to python, I have created a script where expected outputs should be able to get user that has globalRole = Admin and user with tprojectRole = Admin. I am getting the values in the json file which it is a dictionary. How can I get my expected values below after my example no. 3? Thanks
Below are examples of JSON file that I'm parsing.
"firstName": "User",
"lastName": "Last",
"emailAddress": "[email protected]",
"locale": "en_GB",
"isActive": "1",
"defaultTestprojectID": "",
"globalRole": {
"name": "admin",
"rights": [
{
"name": "testplan_execute",
"dbID": "1"
},
{
"name": "testplan_create_build",
"dbID": "2"
},
],
"dbID": "10"
},
"globalRoleID": "10",
"tprojectRoles": "",
This is the other example of JSON file where tprojectRoles has value but globalRole is not an admin.
"firstName": "User2",
"lastName": "Last2",
"emailAddress": "[email protected]",
"locale": "en_GB",
"isActive": "1",
"defaultTestprojectID": "",
"globalRole": {
"name": "viewer",
"rights": [
{
"name": "testplan_execute",
"dbID": "1"
},
{
"name": "testplan_create_build",
"dbID": "2"
},
],
"dbID": "10"
},
"globalRoleID": "10",
"tprojectRoles": {
"1456505": {
"name": "admin",
}
Example no. 3, if both global role and projectrole is Admin it should display both.
"firstName": "User3",
"lastName": "Last3",
"emailAddress": "[email protected]",
"locale": "en_GB",
"isActive": "1",
"defaultTestprojectID": "",
"globalRole": {
"name": "admin",
"rights": [
{
"name": "testplan_execute",
"dbID": "1"
},
{
"name": "testplan_create_build",
"dbID": "2"
},
],
"dbID": "10"
},
"globalRoleID": "10",
"tprojectRoles": {
"1456505": {
"name": "admin",
}
Now, How to get this expected values?
User Last GlobalRole: Admin
User2 Last2 GlobalRole: Viewer ProjectRole: Admin
User3 Last3 GlobalRole: Admin ProjectRole: Admin
This is my script the I'm working on:
for admin in tls.getUserByID(loginID): #getUserByID API
firstName = admin.get('firstName')
lastName = admin.get('lastName')
globalRole = admin.get('globalRole')
projectRole = admin.get('tprojectRoles')
user_active = ('Active')
user_inactive = ('Inactive')
if str(globalRole['name']) == "admin" or str(projectRole) == "":
print(loginID, loginName, firstName, str(globalRole['name']))
else:
for projKey,projRole in projectRole.items():
if "admin" in str(projRole['name']):
print(loginID, loginName, firstName, str(globalRole['name']), projKey, projRole['name'])
CodePudding user response:
This is one way to extract the data from the projectRoles
field.
for admin in admins:
text = admin["firstName"] " " admin["lastName"] " GlobalRole: "
grole = admin["globalRole"]["name"]
if grole == "admin":
if not admin["tprojectRoles"]:
print(text grole)
continue
prole = list(admin["tprojectRoles"].items())[0][1]["name"]
print(text grole " ProjectRole: " prole)