Question

Difficulty: MediumInterpreting JSON Encoded Data

A network engineer issues a RESTCONF GET request to a Cisco IOS XE router to inspect interface operational state data. The API returns the following JSON payload, which is parsed into a Python dictionary named `payload`:

{
"ietf-interfaces:interfaces-state": {
"interface": [
{
"name": "GigabitEthernet1",
"type": "iana-if-type:ethernetCsmacd",
"oper-status": "up",
"phys-address": "00:50:56:89:a1:b2",
"speed": 1000000000
},
{
"name": "GigabitEthernet2",
"type": "iana-if-type:ethernetCsmacd",
"oper-status": "down",
"phys-address": "00:50:56:89:a1:c3",
"speed": 100000000
}
]
}
}

Match each Python dictionary lookup expression on the left with its evaluated value on the right.

  • payload["ietf-interfaces:interfaces-state"]["interface"][0]["oper-status"]"up"
  • payload["ietf-interfaces:interfaces-state"]["interface"][1]["speed"]100000000
  • payload["ietf-interfaces:interfaces-state"]["interface"][1]["oper-status"]"down"
  • payload["ietf-interfaces:interfaces-state"]["interface"][0]["speed"]1000000000

Answer

The correct pairings match expression index [0] to GigabitEthernet1 values ('up' and 1000000000) and index [1] to GigabitEthernet2 values ('down' and 100000000).
In Python, JSON arrays are represented as lists, which use zero-based indexing. The expression referencing index [0] targets the first interface entry ('GigabitEthernet1'), where 'oper-status' is 'up' and 'speed' is 1000000000. The expression referencing index [1] targets the second interface entry ('GigabitEthernet2'), where 'oper-status' is 'down' and 'speed' is 100000000.

Step-by-Step Solution

1
Locate the outer key path
Accessing payload['ietf-interfaces:interfaces-state']['interface'] retrieves a JSON array containing two interface dictionary objects.
JSON arrays are zero-indexed list structures in Python.
2
Evaluate index [0] dictionary key queries
Index [0] corresponds to GigabitEthernet1. Fetching 'oper-status' yields 'up' and fetching 'speed' yields 1000000000.
Array position 0 accesses the first element in the interface array.
3
Evaluate index [1] dictionary key queries
Index [1] corresponds to GigabitEthernet2. Fetching 'speed' yields 100000000 and fetching 'oper-status' yields 'down'.
Array position 1 accesses the second element in the interface array.

Key Concept

Interpreting JSON Encoded Data and Indexing Nested Structures in Automation Scripts
Rate this question