How to extract nested JSON data?

后端 未结 4 2030
野的像风
野的像风 2020-12-07 06:23

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 :

4条回答
  •  我在风中等你
    2020-12-07 07:14

    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']
    

提交回复
热议问题