1

I am currently trying to automize some stuff with the API that exoscale (a cloudprovider) provides. When I send a specific API request to exoscale i get the following answer:

{
"count": 1,
"virtualmachine": [
    {
        "account": "[email protected]",
        "cpunumber": 1,
        "cpuspeed": 2198,
        "cpuused": "0.22%",
        "created": "2013-12-19T22:28:31+0100",
        "displayname": "vagrant_1387488511",
        "guestosid": "113038d0-a8cd-4d20-92be-ea313f87c3ac",
        "haenable": false,
        "hypervisor": "KVM",
        "id": "8e5fe56d-6d8a-4210-b826-73327b9385dc",
        "keypair": "vagrant",
        "memory": 512,
        "name": "8e5fe56d-6d8a-4210-b826-73327b9385dc",
        "networkkbsread": 10060,
        "networkkbswrite": 5443,
        "nic": [
            {
                "gateway": "185.19.28.1",
                "id": "672bf4f3-8ed0-4581-9b3d-b6cd4c4fe8e6",
                "ipaddress": "185.19.28.148",
                "isdefault": true,
                "macaddress": "06:78:f8:00:00:d1",
                "netmask": "255.255.254.0",
                "networkid": "00304a04-c7ea-4e77-a786-18bc64347bf7",
                "traffictype": "Guest",
                "type": "Shared"
            }
        ],
        "passwordenabled": true,
        "rootdeviceid": 0,
        "rootdevicetype": "Filesystem",
        "securitygroup": [
            {
                "description": "Default Security Group",
                "id": "b87ff34d-abf3-42d3-a097-78075515dd34",
                "name": "default"
            }
        ],
        "serviceofferingid": "71004023-bb72-4a97-b1e9-bc66dfce9470",
        "serviceofferingname": "Micro",
        "state": "Stopped",
        "tags": [],
        "templatedisplaytext": "Linux Ubuntu 12.04 LTS 64-bit 10GB Disk",
        "templateid": "a17b40d6-83e4-4f2a-9ef0-dce6af575789",
        "templatename": "Linux Ubuntu 12.04 LTS 64-bit",
        "zoneid": "1128bd56-b4d9-4ac6-a7b9-c715b187ce11",
        "zonename": "CH-GV2"
    }
]
}

How do I access the "description" element ( in "securitygroup") in the strcuture in python and is there a method to "search" for a specific key and get the value back in order to store it in an variable?

1 Answer 1

1

You can use the built-in Module json:

import json
x = json.loads(stringVarHere)["virtualmachine"][0]["securitygroup"][0]["description"]
print(x)

Output:

Default Security Group
Sign up to request clarification or add additional context in comments.

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.