Python Forum
Error while checking for key in Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error while checking for key in Dictionary
#1
Hi,

I am trying below code to get values from dict
My sample dictionary look like this


{'value': [{'id': '/subscriptions/xxxxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Compute/disks/xxxxxxx', 'name': 'xxxxxxx', 'type': 'Microsoft.Compute/disks', 'sku': {'name': 'Premium_LRS', 'tier': 'Premium'}, 'managedBy': '/subscriptions/xxxxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Compute/virtualMachines/xxxxx', 'location': 'ukwest', 'createdTime': '2020-07-01T19:42:12.1380255Z', 'changedTime': '2020-07-01T19:52:12.6457358Z'},{'id': '/subscriptions/xxxxx/resourceGroups/xxxxxx/providers/Microsoft.Storage/storageAccounts/xxxxx', 'name': 'xxxxxxx', 'type': 'Microsoft.Storage/storageAccounts', 'sku': {'name': 'Standard_LRS', 'tier': 'Standard'}, 'kind': 'Storage', 'location': 'ukwest', 'createdTime': '2020-07-01T19:41:49.5513608Z', 'changedTime': '2020-07-01T19:52:10.2317633Z',  'tags': {'managed': 'nob'},{'createdby': 'bob'}}}]
so here the requrirement is :

1) I need to find out tags for each dictionary and also only while doing so I need to get only 'managed' key from tags.
2) If 'managed' key is null then, display value as 'Not Tagged'
3) If 'tags' key itself doesnot exist in dictionary above then, it should display "Tags key missing"

  
    status = json.loads(response.text)
    print(type(status)) # this is a dict
    #print(status)
    resource_type = status['value'][0]['type']
    region = status['value'][0]['location']
    created_time = status['value'][0]['createdTime']
    tag = status.get(['value'][0]['tags'])
    tags_info = {"Resource_Type": resource_type, "Region": region, "Created_Time": created_time, "Tags": tag}
    print(tags_info)
but i am getting below error for one of the key

    tag = status.get(['value'][0]['tags'])
TypeError: string indices must be integers
Tried this as well:

    status = json.loads(response.text)
    items = status.items()
    tags = items['value']['tags']
    print(tags)
gives error


I am using above code as part of for loop, hence some of the item in loop does not have this tags, where as some have. so tried to use get function to check if key exist or not but getting error as above

can some one suggest correction here..thanks in advance
Reply


Messages In This Thread
Error while checking for key in Dictionary - by onenessboy - Aug-13-2020, 05:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Proxy Checking Software Error rizzla 5 2,659 Jan-19-2021, 09:40 AM
Last Post: rizzla
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 1,966 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  Is it possible to check for a key error in a python dictionary? mrsenorchuck 0 1,489 Dec-06-2019, 11:38 PM
Last Post: mrsenorchuck
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 3,971 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Calculation Inside Dictionary Error prophet11 3 2,633 Apr-22-2019, 05:23 AM
Last Post: perfringo
  Dictionary Results Error prophet11 2 2,298 Apr-21-2019, 11:22 PM
Last Post: prophet11
  Error 'object has no attribute' when iterating thru a dictionary mrapple2020 8 7,038 Apr-08-2019, 01:36 AM
Last Post: mrapple2020

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020