1

I have a banch of servers and I use API to extract data about storage. To get details about storage uuid (named storage in json) I use code as below:

srvdet = requests.get('https://hhh.com/1.2/server/76856585', auth=HTTPBasicAuth('login', 'pass'))
srvdet_json = srvdet.json()
datas = srvdet.json()
print(datas)

And result in json:

{
   "server": {
      "boot_order": "",
      "core_number": "2",
      "created": ,
      "firewall": "off",
      "host": ,
      "hostname": "hello",
      "ip_addresses": {
         "ip_address": [
            {
               "access": "private",
               "address": "",
               "family": ""
            },
            {
               "access": "",
               "address": "",
               "family": "",
               "part_of_plan": ""
            }
         ]
      },
      "license": 0,
      "memory_amount": "",
      "nic_model": "",
      "plan": "",
      "plan_ipv4_bytes": "",
      "plan_ipv6_bytes": "0",
      "state": "started",
      "storage_devices": {
         "storage_device": [
            {
               "address": "",
               "boot_disk": "0",
               "part_of_plan": "",
               "storage": "09845",
               "storage_size": ,
               "storage_title": "",
               "type": ""
  
}

For now it works perfectly fine. The problem is when I need to get "09845" which is value for storage. When I try to use this code:

for storage in datas['server']['storage_devices']['storage_device']:
    srvstorage = storage
    print(srvstorage)

The result is:

{'storage': '09845', 'storage_size':, 'address': '', 'boot_disk': '0', 'type': '', 'part_of_plan': 'yes', 'storage_title': ''}

What am I doing wrong? How to save "09845" in variable?

EDIT:

Now I get error when trying to access details about storage. I want to extract backup status which is state in json:

{
   "storage": {
      "access": "private",
      "backup_rule": {},
      "backups": {
         "backup": []
      },
      "license": 0,
      "part_of_plan": "",
      "servers": {
         "server": [
            ""
         ]
      },
      "size": ,
      "state": "online",
      "tier": "",
      "title": "",
      "type": "",
      "uuid": "",
      "zone": ""
   }
}

When I execute this code:

bkpdet = requests.get('https://fffff.com/1.2/storage/08475', auth=HTTPBasicAuth('login', 'pass'))
bkpdet_json = bkpdet.json()
datastg = bkpdet.json()
print(datastg)
for sts in datastg['storage']:
    bkpsts = sts['state']
    print(bkpsts)

I get this error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: string indices must be integers

The whole idea is to at the end get info about status using this code:

if bkpsts == "online":
    print('Backup has been created.')
else bkpsts == "backuping":
    print('Backup creation is in progress.')
else:
    print(bkpdet.status_code)

I was searching but still can't find what is wrong here.

2
  • I Guess you don't need this line datastg = bkpdet.json() have you already parse it to json. Then replace datastg with bkpdet_json `. Commented Oct 29, 2019 at 14:28
  • 1
    I found the solution for edit part of my post is here: <stackoverflow.com/questions/58609001/…> Commented Oct 29, 2019 at 18:38

2 Answers 2

1

You miss a key when you want to access storage, you loop over it and it's fine. But in each iteration you get a dictionary from which you need to call the right key, in your case storage.

for storage in datas['server']['storage_devices']['storage_device']: 
    srvstorage = storage.get("storage", None) 
    print(srvstorage)

Note

get method will be preferable to use as you may encounter a device with out storage information inside, by using get you can avoid the KeyError.

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

2 Comments

This is it! Thank you so much! :D
@Brzozova Perfect, feel free to validate this answer by clicking on the check mark button under the vote one.
1

You can just do

for device in datas['server']['storage_devices']['storage_device']:
    device_storage = device['storage']
    print(device_storage)

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.