Soru

Zorluk: OrtaInterpreting JSON Encoded Data

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?

  1. data["response"]["devices"][1]["mgmt_ip"]Cevap
  2. B
    data["response"]["devices"][2]["mgmt_ip"]
  3. C
    data["response"]["devices"]["ACCESS"]["mgmt_ip"]
  4. D
    data["response"][1]["mgmt_ip"]

Cevap

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".

Adım Adım Çözüm

1
Analyze the top-level structure of the JSON payload.
The root is a dictionary/object containing the top-level key "response". Accessing `data["response"]` yields an inner dictionary.
Top-level keys must be referenced first.
2
Locate the nested key containing the list of devices.
Inside `data["response"]`, the key "devices" holds a JSON array (Python list) of device dictionary objects.
The array is stored under the "devices" key within the response object.
3
Determine the zero-based list index for the ACCESS device object.
The DISTRIBUTION device is at index 0, and the ACCESS device is at index 1.
JSON arrays correspond to Python zero-indexed lists.
4
Extract the target key value from the selected device dictionary.
Accessing `["mgmt_ip"]` on index 1 yields "10.1.20.1".
The dictionary key storing the IP address is "mgmt_ip".

Anahtar Kavram

Traversing nested JSON structures and combining dictionary key references with zero-based list array indexing in Python.
Bu soruyu puanla