How to read individual values from a response variable?

Ger It 20 Reputation points
2024-02-04T19:33:45.5133333+00:00

Disclaimer: I am really really new with Python and MSGraph I have the folllowing response variable:

response = requests.get(
    #url="https://graph.microsoft.com/v1.0/users",
    url = f"https://graph.microsoft.com/v1.0/users?$count=true&$search={email_address}&$orderBy=displayName&$select=id,displayName,mail",
    headers=headers,
)
#print(json.dumps(response.json(), indent=4))
print(response.text)

And the output is

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(id,displayName,mail)",
    "@odata.count": 1,
    "value": [
        {
            "id": "#######",
            "displayName": "Firstname Lastname",
            "mail": "******@domain.com"
        }
    ]
}

or if I do the .txt

{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users(id,displayName,mail)","@odata.count":1,"value":[{"id":"########","displayName":"Firstname Lastname","mail":"******@domain.com"}]}

I wonder how to read the different values, like displayName or mail and store them into a variable. Thanks for your help

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Deepanshu Sharma 500 Reputation points Microsoft External Staff
    2024-02-12T02:23:15.8133333+00:00

    Hello, If the response is in json you could do something like (python3):

    import json
    import requests as reqs
    
    # Make the HTTP request.
    response = reqs.get('
    
    # Use the json module to load CKAN's response into a dictionary.
    response_dict = json.loads(response.text)
    
    for i in response_dict:
        print("key: ", i, "val: ", response_dict[i])
    
    

    To see everything in the response you can use .__dict__:

    print(response.__dict__)
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.