简体   繁体   中英

looping and counting json response in python

I have the below json reponse:

{

"routers":[
    {
        "status":"ACTIVE",
        "external_gateway_info":{
            "network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
            "enable_snat":true,
            "external_fixed_ips":[
                {
                    "subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
                    "ip_address":"194.168.0.21"
                }
            ]
        },
        "name":"test_router3",
        "admin_state_up":true,
        "tenant_id":"f19408f548234b71a5052549cb89ae83",
        "distributed":false,
        "routes":{
        },
        "ha":false,
        "id":"102fc36a-e2f7-427f-983d-f71b77be0beb"
    },
    {
        "status":"ACTIVE",
        "external_gateway_info":{
            "network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
            "enable_snat":true,
            "external_fixed_ips":[
                {
                    "subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
                    "ip_address":"194.168.0.20"
                }
            ]
        },
        "name":"test_router2",
        "admin_state_up":true,
        "tenant_id":"c3a69d0bbc944b1fbb0873efd71189db",
        "distributed":false,
        "routes":{
        },
        "ha":false,
        "id":"11b413a3-975e-491c-b0cf-9e2ab11a17c4"
    },
    {
        "status":"ACTIVE",
        "external_gateway_info":{
            "network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
            "enable_snat":true,
            "external_fixed_ips":[
                {
                    "subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
                    "ip_address":"194.168.0.18"
                }
            ]
        },
        "name":"test_router0",
        "admin_state_up":true,
        "tenant_id":"82c3cbe17d8e431a945ee972067b4063",
        "distributed":false,
        "routes":{
        },
        "ha":false,
        "id":"17ed98c2-36d3-49ed-b9bb-a51616ae0112"
    },
    {
        "status":"ACTIVE",
        "external_gateway_info":{
            "network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
            "enable_snat":true,
            "external_fixed_ips":[
                {
                    "subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
                    "ip_address":"194.168.0.19"
                }
            ]
        },
        "name":"test_router1",
        "admin_state_up":true,
        "tenant_id":"9930b010b96b44c8844dcac7e5b15682",
        "distributed":false,
        "routes":{
        },
        "ha":false,
        "id":"68135a8f-a28c-43c3-9609-8a23b7b7452e"
    },
    {
        "status":"ACTIVE",
        "external_gateway_info":{
            "network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
            "enable_snat":true,
            "external_fixed_ips":[
                {
                    "subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
                    "ip_address":"194.168.0.22"
                }
            ]
        },
        "name":"test_router4",
        "admin_state_up":true,
        "tenant_id":"f844661b3a7d413a8d66a12e77833539",
        "distributed":false,
        "routes":{
        },
        "ha":false,
        "id":"6bbb58df-2699-4428-a95e-de51ed4cb37b"
    },
    {
        "status":"ACTIVE",
        "external_gateway_info":{
            "network_id":"4eeac810-19b5-4b9e-82aa-85766506aeec",
            "enable_snat":true,
            "external_fixed_ips":[
                {
                    "subnet_id":"f5d85de1-dd6d-4b56-8ed4-7a2938f6259a",
                    "ip_address":"194.168.0.3"
                }
            ]
        },
        "name":"external_router1",
        "admin_state_up":true,
        "tenant_id":"c3a69d0bbc944b1fbb0873efd71189db",
        "distributed":false,
        "routes":{
        },
        "ha":false,
        "id":"dd7a4123-4b5a-49df-bc42-db71772cffd0"
    }
]

}

I need to get the following:

  1. count of 'id' in the whole response

Please could any one tell how to loop through json.

I tried for loop but get error "integers exepected as index"

Assuming you want the number of elements in the 'routers' key of the json dictionary:

import json
response = json.reads(response) # Given a json string, returns a python dictionary
return len(response['routers'])

Here you have a snippet that counts the appears of 'id' property of route items

>>> import json
>>> json_data = json.loads(raw_json_data)
>>> id_count = sum(1 for route in json_data['routers'] if route.get('id'))
>>> 
>>> id_count
6   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM