Question

Difficulty: HardInterpreting JSON Encoded Data

An network engineer issues a REST API call to a Cisco DNA Center controller to retrieve information regarding managed network devices. The controller returns the following JSON response payload:

{
"response": [
{
"family": "Switches and Hubs",
"hostname": "Dist-Switch-01",
"managementIpAddress": "192.168.10.1",
"upTime": "12 days, 04:12:00",
"interfaceList": [
{
"portName": "GigabitEthernet1/0/1",
"vlan": 10,
"status": "up",
"speed": 1000
},
{
"portName": "GigabitEthernet1/0/2",
"vlan": 20,
"status": "down",
"speed": 1000
}
]
},
{
"family": "Routers",
"hostname": "Edge-Router-01",
"managementIpAddress": "10.1.1.1",
"upTime": "45 days, 11:05:22",
"interfaceList": [
{
"portName": "GigabitEthernet0/0/0",
"vlan": 1,
"status": "up",
"speed": 10000
}
]
}
],
"version": "1.0"
}

Assuming the variable `data` holds the parsed Python dictionary representation of this JSON payload, which Python expression correctly extracts the operating status (`"status"`) of the second interface on the distribution switch (`Dist-Switch-01`)?

  1. data["response"][0]["interfaceList"][1]["status"]Answer
  2. B
    data["response"][1]["interfaceList"][2]["status"]
  3. C
    data["response"][0]["interfaceList"][2]["status"]
  4. D
    data["response"]["interfaceList"][0][1]["status"]

Answer

data["response"][0]["interfaceList"][1]["status"]
The top-level JSON structure is an object mapped to a Python dictionary containing the key 'response'. The value of 'response' is a list of device objects. The distribution switch ('Dist-Switch-01') is the first element in this list, which corresponds to index 0. Within this dictionary, 'interfaceList' holds a list of interface objects. The second interface ('GigabitEthernet1/0/2') is located at index 1 of this list. Accessing the key 'status' on this dictionary yields the desired value.

Step-by-Step Solution

1
Identify the top-level structure and navigate to the 'response' array
The top-level JSON element is an object (dictionary). Accessing data["response"] returns a list containing two device dictionary objects.
Top-level JSON curly braces {} map to a Python dictionary where keys are string names.
2
Select the distribution switch object from the 'response' array using zero-based indexing
The distribution switch ('Dist-Switch-01') is the first item in the list, accessed via index 0: data["response"][0].
JSON square brackets [] map to Python lists, which use zero-based indexing (0 is the 1st item).
3
Navigate to the 'interfaceList' key within the distribution switch dictionary
Accessing data["response"][0]["interfaceList"] returns a list of interface dictionaries.
The key 'interfaceList' inside the device object points to an array of interface dictionaries.
4
Select the second interface and extract the 'status' key value
The second interface ('GigabitEthernet1/0/2') is at index 1 of the list, and its status is extracted via data["response"][0]["interfaceList"][1]["status"].
The second element in a zero-indexed array has an index of 1.

Key Concept

Interpreting JSON Encoded Data and Accessing Nested Data Structures
Estimated Time:1m 30s
Rate this question