Python Forum
Error while checking for key in Dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error while checking for key in Dictionary (/thread-29005.html)



Error while checking for key in Dictionary - onenessboy - Aug-13-2020

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


RE: Error while checking for key in Dictionary - ndc85430 - Aug-13-2020

Are you passing the right thing to get? ['value'][0] gives you the string 'value' and then you're trying to look up 'tags' from that, which clearly doesn't make sense.


RE: Error while checking for key in Dictionary - bowlofred - Aug-13-2020

Accessing a sub-element via direct access:
>>> d['a']['b']
5
Accessing a sub-element via multiple get() calls:
>>> d.get('a').get('b')
5



RE: Error while checking for key in Dictionary - onenessboy - Aug-14-2020

(Aug-13-2020, 05:29 PM)ndc85430 Wrote: Are you passing the right thing to get? ['value'][0] gives you the string 'value' and then you're trying to look up 'tags' from that, which clearly doesn't make sense.

Hi,

I have updated original post with sample data, and requriments clear.. can you please help


RE: Error while checking for key in Dictionary - ndc85430 - Aug-14-2020

(Aug-13-2020, 05:20 PM)onenessboy Wrote:
    status = json.loads(response.text)
    items = status.items()
    tags = items['value']['tags']
    print(tags)

One question to help you work it out: what type is items? Perhaps you should print it out if you're not sure.


RE: Error while checking for key in Dictionary - onenessboy - Aug-14-2020

(Aug-14-2020, 11:42 AM)ndc85430 Wrote:
(Aug-13-2020, 05:20 PM)onenessboy Wrote:
    status = json.loads(response.text)
    items = status.items()
    tags = items['value']['tags']
    print(tags)

One question to help you work it out: what type is items? Perhaps you should print it out if you're not sure.

Yeah tried with
print(type(items))
it returned class dict

I even tried this

tags = items['value']['tags']
replaced with
tags = get('tags')

then it returned
Nonetype error