Aug-13-2020, 05:20 PM
(This post was last modified: Aug-14-2020, 09:11 AM by onenessboy.)
Hi,
I am trying below code to get values from dict
My sample dictionary look like this
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"
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
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 integersTried 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