-1

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 .

haduki
  • 77
  • 9
  • 1
    Does this answer your question? [Convert JSON string to dict using Python](https://stackoverflow.com/questions/4528099/convert-json-string-to-dict-using-python) – Kenny May 11 '22 at 09:41
  • I couldnt figure out the correct syntax. thanks a lot now it works – haduki May 11 '22 at 09:59

2 Answers2

3

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

x = x.json()
print(x["organization"]["id"])
alanturing
  • 86
  • 6
0

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 ?

haduki
  • 77
  • 9