A Python script queries a Cisco Catalyst Center API endpoint for network device inventory and stores the resulting data structure in a variable named `data`. The JSON payload structure is shown below:
{
"response": {
"devices": [
{
"id": "dev-101",
"role": "DISTRIBUTION",
"mgmt_ip": "10.1.10.1"
},
{
"id": "dev-102",
"role": "ACCESS",
"mgmt_ip": "10.1.20.1"
}
],
"totalCount": 2
}
}
Which Python expression correctly extracts the management IP address of the ACCESS role device?
- data["response"]["devices"][1]["mgmt_ip"]Answer
- Bdata["response"]["devices"][2]["mgmt_ip"]
- Cdata["response"]["devices"]["ACCESS"]["mgmt_ip"]
- Ddata["response"][1]["mgmt_ip"]
Answer
data["response"]["devices"][1]["mgmt_ip"] correctly extracts the management IP address "10.1.20.1".
The JSON structure consists of a outer dictionary with a key named "response", which contains another dictionary with a key named "devices". The value of "devices" is a list (array) of dictionaries. In Python, list indexing begins at 0. Therefore, the first element (DISTRIBUTION device) is at index 0, and the second element (ACCESS device) is at index 1. Accessing the "mgmt_ip" key on list element index 1 yields "10.1.20.1".
Step-by-Step Solution
Key Concept
Traversing nested JSON structures and combining dictionary key references with zero-based list array indexing in Python.