2

I get error when trying to access details about servers storage using API. 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

How can I access state*? 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 cannot find what is wrong here.

0

3 Answers 3

2

When you use:

for sts in datastg['storage']:

sts will be a string key. You are trying to treat it like an dictionary.

If you just need the state value, you can access it directly:

datastg['storage']['state']

If you want to iterate all key value pairs under storage, you can use items() to both key and value.

for key, value in datastg['storage'].items():
    print(key,":", value)
Sign up to request clarification or add additional context in comments.

Comments

1

As @MarkMeyer advised I changed code like this:

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

It works perfectly!

Comments

0

The code below works

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

state = data['storage']['state']
print(state)

output

online

1 Comment

It's not working for me, I get an error: >>> state = data['storage']['state'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'storage' >>> print(state) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'state' is not defined

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.