'Access a json field in python [duplicate]

Im using a get request in python

x = requests.get(url, headers = ... ) 
print(x.json())

and the result im getting is

{
'organization': {
'id': '6', 
'name': 'TestPostman', 
'displayName': 'TestPostman', 
'canHaveGateways': True, 
'maxGatewayCount': 0, 
'maxDeviceCount': 0
},
'createdAt': '2022-05-10T18:07:49.327175Z', 
'updatedAt': '2022-05-10T19:44:09.667978Z'
}

How can i access any of the organization's fields ? I tried x.organizzation['id'] but does not work .



Solution 1:[1]

If you want to access the ID in the Organization try this:

x = x.json()
print(x["organization"]["id"])

Solution 2:[2]

Similar approach but its a bit different this time. the output im getting is

{
    "totalCount": "1",
    "result": [
        {
            "id": "62",
            "name": "HOME",
            "displayName": "HOME",
            "canHaveGateways": true,
            "createdAt": "2022-05-12T13:50:10.303205Z",
            "updatedAt": "2022-05-12T13:50:10.303205Z"
        }
    ]
}

I tried print(x["result"]["id"]) but due to the fact result is like this result[{}] it does not work . Any idea about that ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alanturing
Solution 2 haduki