Question

Difficulty: Very hardInterpreting JSON Encoded Data

A network automation engineer issues an HTTP GET request to a Cisco IOS-XE RESTCONF API endpoint to retrieve BGP neighbor status details. The router returns the following JSON-encoded response:

{
"Cisco-IOS-XE-bgp:bgp-state-data": {
"bgp-neighbors": [
{
"neighbor-id": "192.168.10.1",
"remote-as": 65001,
"state": "Established",
"address-family": [
{
"af-name": "ipv4-unicast",
"prefixes": {
"accepted": 42,
"sent": 15
}
},
{
"af-name": "vpnv4-unicast",
"prefixes": {
"accepted": 120,
"sent": 88
}
}
]
},
{
"neighbor-id": "192.168.20.2",
"remote-as": 65002,
"state": "Active",
"address-family": [
{
"af-name": "ipv4-unicast",
"prefixes": {
"accepted": 0,
"sent": 0
}
}
]
}
]
}
}

Match each Python dictionary lookup / data extraction expression on the left to its corresponding value or evaluated data type on the right based on the JSON response payload.

  • data["Cisco-IOS-XE-bgp:bgp-state-data"]["bgp-neighbors"][0]["address-family"][1]["prefixes"]["accepted"]120
  • data["Cisco-IOS-XE-bgp:bgp-state-data"]["bgp-neighbors"][1]["remote-as"]65002 (integer)
  • type(data["Cisco-IOS-XE-bgp:bgp-state-data"]["bgp-neighbors"][0]["neighbor-id"])str (string)
  • data["Cisco-IOS-XE-bgp:bgp-state-data"]["bgp-neighbors"][0]["address-family"][0]["af-name"]"ipv4-unicast"

Answer

Each Python expression correctly matches its evaluated output: the nested lookup for the second address family accepted prefixes of neighbor 0 evaluates to 120; the remote-as of neighbor 1 evaluates to integer 65002; the data type of the quoted neighbor-id string evaluates to str; and the first address family name of neighbor 0 evaluates to "ipv4-unicast".
Each expression matches its exact JSON path evaluation according to zero-based array indexing rules and standard JSON data type mapping into Python data structures (objects to dictionaries, lists to arrays, quoted values to strings, unquoted numbers to integers).

Step-by-Step Solution

1
Parse the top-level JSON structure and array indices using zero-based index rules.
Neighbor array index [0] corresponds to neighbor '192.168.10.1'. Neighbor array index [1] corresponds to neighbor '192.168.20.2'.
JSON arrays listed under brackets [] in Python parsing are zero-indexed.
2
Trace nested path queries down into key-value pairs and inner arrays.
Path 1 selects neighbor [0] ('192.168.10.1'), address-family array index [1] ('vpnv4-unicast'), and extracts key 'accepted' with value 120. Path 2 selects neighbor [1] ('192.168.20.2') and extracts key 'remote-as' with value 65002.
Proper path traversal requires following object keys ({}) and array indices ([]) sequentially.
3
Distinguish between JSON primitive types (string vs number/integer).
'neighbor-id' value "192.168.10.1" is enclosed in quotes (string), whereas 'remote-as' value 65002 is unquoted (integer).
JSON represents strings in double quotes and numbers without quotes.

Key Concept

JSON data structure traversal and type interpretation (arrays, objects, strings, integers)
Rate this question