-3
"metadata": {
"kind": "compute#metadata",
"items": [
{
"key": "serial-port-logging-enable",
"value": "true"
},
{
"key": "cluster-name",
"value": "cluster-1"
},

I want to print the value of value, when the key is "cluster-name"

So here I want to print cluster-1 from the above data.

Can someone help me with what I can do?


My try

cluster_name = (response['metadata']['items'][0]('key'))
azro
  • 53,056
  • 7
  • 34
  • 70
  • What was the problem? Do you know how to access values in lists given an index? Do you know how to access values in dictionaries given a key? Is this a string? Did you already parse it as JSON (it looks like it is JSON)? – mkrieger1 Mar 27 '22 at 14:02
  • Yes i want to know how to access the nested value here. Also, I only want the nester value for a given key – kunal Mar 27 '22 at 14:08
  • @kunal Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – Cubix48 Mar 27 '22 at 14:10
  • This is what i tried but I am not getting the output I need for my requirement cluster_name = (response['metadata']['items'][0]('key')) – kunal Mar 27 '22 at 14:14

2 Answers2

0

If you don't know the position, you need to iterate to fine the valid item

response = {
    "metadata": {
        "kind": "compute#metadata",
        "items": [
            {"key": "serial-port-logging-enable", "value": "true"},
            {"key": "cluster-name", "value": "cluster-1"}]
    }}

for item in response['metadata'].get('items', []):
    if item["key"] == "cluster-name":
        print(item['value'])
        break
else:
    print(None)

edit :

  • added .get('items', []) in case the key doesn't exist
  • added break/else to print None, in case there is no match
azro
  • 53,056
  • 7
  • 34
  • 70
  • I am getting the below error cluster_name = (response['metadata']['items'][1]('value')) IndexError: list index out of range – kunal Mar 27 '22 at 14:24
  • @kunal the code of my answer works. So if it doesn't with your response, then it means that you didn't show us the real response content – azro Mar 27 '22 at 14:26
  • actually the key": "cluster-name" will not always be at index 1 and can be at different position. I want to get the value of "key": value wherever I have key: cluster-name – kunal Mar 27 '22 at 14:27
  • @kunal oh ok, see my edit – azro Mar 27 '22 at 14:30
  • I am seeing the below error with your edited code KeyError: 'items' – kunal Mar 27 '22 at 14:42
  • @kunal please make some effort, try to understand my code, the logic regalativly to the example dict I show, then compare to yours and try to fix it. If you want to develop learn a bit, you can't only wait for other to build your exact need (also if don't you give the good example) – azro Mar 27 '22 at 15:51
  • I understand your code. I am just trying to see how to handle the situation where the key "items" is not present. the code does work where the key is present. However where key is not present I am facing the below error for item in response["metadata"]["items"]: KeyError: 'items' – kunal Mar 27 '22 at 16:12
  • I really appreciate all your help with this. I did try with the code you edited now earlier as well. However, my issue is with this code it just prints the output where the key items is present. My requirement is to print "cluster-name" as None where this key is not present – kunal Mar 27 '22 at 16:26
  • @kunal and you can't do that alone too ? – azro Mar 27 '22 at 16:46
  • I tried the below code, but still dont see the output where they key isnt present as None for item in response['metadata'].get('items', []): if item["key"] == "cluster-name": cluster_name = item.get('value', None) – kunal Mar 27 '22 at 16:56
  • @kunal see my edit with break/else – azro Mar 28 '22 at 05:46
0

You can try below :

response['metadata']['items'][1]['value']
Vijay
  • 96
  • 3