Soru

Zorluk: OrtaInterpreting JSON Encoded Data

An automated Python monitoring script sends a RESTCONF HTTP GET request to a Cisco IOS XE router to inspect BGP peer relationships. The JSON response is parsed into a Python dictionary named `bgp_data` as shown below:

{
"Cisco-IOS-XE-bgp:bgp": {
"bgp": [
{
"asn": 65001,
"neighbor": [
{
"ip": "10.1.1.2",
"remote-as": 65002,
"state": "Established"
},
{
"ip": "10.2.2.2",
"remote-as": 65003,
"state": "Active"
}
]
}
]
}
}

Which Python expression correctly evaluates to the string `"Active"`?

  1. bgp_data["Cisco-IOS-XE-bgp:bgp"]["bgp"][0]["neighbor"][1]["state"]Cevap
  2. B
    bgp_data["Cisco-IOS-XE-bgp:bgp"]["bgp"][1]["neighbor"][1]["state"]
  3. C
    bgp_data["Cisco-IOS-XE-bgp:bgp"]["bgp"][0]["neighbor"][2]["state"]
  4. D
    bgp_data["Cisco-IOS-XE-bgp:bgp"]["bgp"]["neighbor"][1]["state"]

Cevap

bgp_data["Cisco-IOS-XE-bgp:bgp"]["bgp"][0]["neighbor"][1]["state"]
The correct expression traverses the top-level dictionary key `"Cisco-IOS-XE-bgp:bgp"`, selects the single element in the `"bgp"` array using index `0`, accesses the second object in the `"neighbor"` array using index `1`, and reads the string value associated with the `"state"` key, correctly yielding `"Active"`.

Adım Adım Çözüm

1
Identify the top-level dictionary key
Access `bgp_data["Cisco-IOS-XE-bgp:bgp"]`, which contains a dictionary with the key `"bgp"`.
Top-level object wrapping is used in YANG-modeled RESTCONF JSON payloads.
2
Navigate the outer array
Access `["bgp"][0]` to reference the first BGP process structure in the list.
Square brackets `[]` signify a JSON array, which is zero-indexed in Python.
3
Locate the target neighbor within the inner array
Access `["neighbor"][1]["state"]` to retrieve the second neighbor object (`10.2.2.2`) and extract its state value.
The first neighbor (`10.1.1.2`) is at index 0, so the second neighbor (`10.2.2.2`) with state `"Active"` is at index 1.

Anahtar Kavram

Traversing nested JSON dictionaries and zero-indexed arrays in REST API responses
Tahmini Süre:1m 0s
Bu soruyu puanla