1

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

2 Answers 2

1

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

1 Comment

now, how can I call "name": "admin" under tprojectRoles:{"7293":{"name": "admin"}
0

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.

Comments

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.