I have created a script where I need to get each details of user that is being generated in JSON. What could be the possible way in order for me display the "name": "admin"
?
Please see script below. `
import xmlrpc.client
import json
class TestlinkAPIClient:
# substitute your server URL Here
SERVER_URL = "https://testserver"
def __init__(self, devKey):
self.server = xmlrpc.client.ServerProxy(self.SERVER_URL)
self.devKey = devKey
def getUserByLogin(self):
for userDetail in self.server.tl.getUserByLogin(dict(devKey=self.devKey,user='user1')):
user_status = int(userDetail.get("isActive")) #json(userDetails.json) value isActive = 1 else isActive = 0 for not active
lastname = userDetail.get("lastName")
firstname = userDetail.get("firstName")
userLogin = userDetail.get("login")
activeStatus = ("Active User")
globalrole = userDetail.get("globalRole")
tproject = userDetail.get("tprojectRoles")
#for role in tproject.values():
for projectID in tproject:
user_detail = (userLogin " " lastname " " firstname " " str(globalrole['name']) " " "ProjectID: " projectID)
print(user_detail)
# substitute your Dev Key Here
client = TestlinkAPIClient("testlinkApiKey")
details = client.getUserByLogin()
with open('./DebugProjectRoleDetails.json', 'w') as f:
json.dump(details , f)
and here is the sample json format, hhere I should get an output of the "name": "admin"
[
{
"firstName": "UserFirst",
"lastName": "UserLast",
"emailAddress": "[email protected]",
"locale": "fr_FR",
"isActive": "1",
"defaultTestprojectID": "",
"globalRole": {
"name": "projectadmin",
}
"globalRoleID": "10",
"tprojectRoles": {
"1234": {
"name": "admin"
}
} ]
expected output:
0001 User projectadmin ProjectID: 1001 projectRoles=viewer
CodePudding user response:
If userDetail is in a JSON file and you want to save it as a dictionary, so you can use the get() method, you need to use json.load:
import json
with open('./user.json') as f:
details = json.load(f)
Since tproject is also a dictionary you need to use get() to extract the role (value) corresponding to the project (key)
for userDetail in details:
user_status = int(userDetail.get("isActive"))
lastname = userDetail.get("lastName")
firstname = userDetail.get("firstName")
userLogin = userDetail.get("login")
activeStatus = ("Active User")
globalrole = userDetail.get("globalRole")
tproject = userDetail.get("tprojectRoles")
# for role in tproject.values():
for projectID in tproject:
role = tproject.get(projectID)
user_detail = f"{userLogin} {lastname} {firstname} {str(globalrole['name'])} ProjectID: {projectID} {role}"
print(user_detail)
ouput using your JSON sample:
None UserLast UserFirst projectadmin ProjectID: 1234 {'name': 'admin'}
CodePudding user response:
I have noticed that it is possible to declare 2 items in a for loop.
dictionary_item_tproject = tproject.items()
for projectID,projectRole in dictionary_item_tproject:
print(userLogin " " lastname " " firstname " " str(globalrole['name']) " " str(projectID) " " str(projectRole['name']))
This solve my problem. Now I am able to parse JSON properly. Thanks everyone.