-1

I am trying to get a value from a data JSON. I have successfully traversed deep into the JSON data and almost have what I need!

Running this command in Python : autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags']

Gives me this :

'Tags': [{'Key': 'Name', 'Value': 'Trove-Dev-Inst : App WebServer'}, {'Key': 'aws:autoscaling:groupName', 'Value': 'CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT'}, {'Key': 'CodeDeployProvisioningDeploymentId', 'Value': 'd-4WTRTRTRT'}, {'Key': 'Environment', 'Value': 'ernie-dev'}]

I only want to get the value "CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT". This is from the key "aws:autoscaling:groupName".

How can I further my command to only return the value "CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"?

martineau
  • 119,623
  • 25
  • 170
  • 301
ErnieAndBert
  • 1,344
  • 3
  • 21
  • 43
  • ```autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags'][1][1] ``` – krishna reddy Sep 10 '19 at 16:59
  • Just wondering why people give me negative points on this? I am a beginner and did do some research before I posted the question - which I hope now will help others. Do you remember when you first started and had nobody to rely upon?! This is a great resource for beginners - keep it that way. – ErnieAndBert Sep 10 '19 at 18:16
  • **See also:** [Can we be nicer to new users?](https://meta.stackexchange.com/questions/9953/could-we-please-be-a-bit-nicer-to-new-users) Welcome to Stackoverflow and good luck in your learning endeavors. – dreftymac Sep 15 '19 at 18:39

4 Answers4

6

Is this the full output? This a dictionary containing a list with nested dictionaries, so you should treat it that way. Suppose it is called:

A = {
    "Tags": [
        {
            "Key": "Name",
            "Value": "Trove-Dev-Inst : App WebServer"
        },
        {
            "Key": "aws:autoscaling:groupName",
            "Value": "CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT"
        },
        {
            "Key": "CodeDeployProvisioningDeploymentId",
            "Value": "d-4WTRTRTRT"
        },
        {
            "Key": "Environment",
            "Value": "ernie-dev"
        }
    ]
}

Your first adress the object, then its key in the dictionary, the index within the list and the key for that dictionary:

print(A['Tags'][1]['Value'])

Output:

CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT

EDIT: Based on what you are getting then you should try:

autoscaling_name = response['Reservations'][0]['Instances'][0]['Tags'][1]['Value']
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
1

You could also use glom it's great for deeply nested functions and has sooo many uses that make complicated nested tasks easy.

For example translating @Celius's answer:

glom(A, 'Tags.1.Value')

Returns the same thing:

CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT

So to answer your original question you'd use:

glom(response, 'Reservations.0.Instances.0.Tags.1.Value')
Jab
  • 26,853
  • 21
  • 75
  • 114
0

The final code for this is -

    tags = response['Reservations'][0]['Instances'][0]['Tags']
    autoscaling_name = next(t["Value"] for t in tags if t["Key"] == "aws:autoscaling:groupName")

This also ensures that if the order of the data is moved in the JSON data it will still find the correct one.

ErnieAndBert
  • 1,344
  • 3
  • 21
  • 43
0

For anyone struggling to get their heads around list comprehensions and iterators, the cherrypicker package (pip install --user cherrypicker) does this sort of thing for you pretty easily:

from cherrypicker import CherryPicker

tags = CherryPicker(response['Reservations'][0]['Instances'][0]['Tags'])

tags(Key="aws:autoscaling:groupName")[0]["Value"].get()

which gives you 'CodeDeploy_Ernie-dev-Autoscaling-Deploy_d-4WTRTRTRT'. If you're expecting multiple values, omit the [0] to get back a list of all values that have an associated "aws:autoscaling:groupName" key.

This is probably all a bit overkill for your question, which can be solved easily with a simple list comprehension. But this approach might come in handy if you need to do more complicated things later, like matching on partial keys only (e.g. aws:* or something more complicated like a regular expression), or you need to filter based on the values in an intermediate layer of the nested object. This sort of task could lead to lots of complicated nested for loops or list comprehensions, whereas with CherryPicker it stays as a simple, potentially one-line command.

You can find out more about advanced usage at https://cherrypicker.readthedocs.io.

big-o
  • 445
  • 4
  • 7