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
#2
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.
Reply
#3
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
Reply
#4
(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
Reply
#5
(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.
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Proxy Checking Software Error rizzla 5 1,715 Jan-19-2021, 09:40 AM
Last Post: rizzla
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 1,398 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,056 Dec-06-2019, 11:38 PM
Last Post: mrsenorchuck
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 2,763 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Calculation Inside Dictionary Error prophet11 3 1,926 Apr-22-2019, 05:23 AM
Last Post: perfringo
  Dictionary Results Error prophet11 2 1,738 Apr-21-2019, 11:22 PM
Last Post: prophet11
  Error 'object has no attribute' when iterating thru a dictionary mrapple2020 8 5,694 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