Soru

Zorluk: OrtaInterpreting JSON Encoded Data

A network administrator queries a Cisco vManage REST API endpoint to retrieve device interface operational details. The API returns the following JSON payload, which is parsed into a Python dictionary variable named `response_data`:

{
"header": {
"generated_at": 1774137600,
"status": 200
},
"data": [
{
"device_id": "10.10.10.1",
"host_name": "Edge-Router-1",
"interfaces": [
{
"name": "GigabitEthernet1",
"admin_status": "up",
"oper_status": "up",
"ip_address": "192.168.10.1",
"mtu": 1500
},
{
"name": "GigabitEthernet2",
"admin_status": "up",
"oper_status": "down",
"ip_address": "10.0.0.2",
"mtu": 1500
}
]
}
]
}

Which two statements regarding data extraction from this JSON payload are correct? (Select two.)

  1. The expression `response_data["data"][0]["host_name"]` evaluates to the string `"Edge-Router-1"`.Cevap
  2. The expression `response_data["data"][0]["interfaces"][1]["name"]` evaluates to the string `"GigabitEthernet2"`.Cevap
  3. C
    The expression `response_data["data"][1]["host_name"]` evaluates to the string `"Edge-Router-1"`.
  4. D
    The expression `response_data["data"][0]["interfaces"][0]["ip_address"]` evaluates to the string `"10.0.0.2"`.

Cevap

The two correct statements are: 1) `response_data["data"][0]["host_name"]` evaluates to `"Edge-Router-1"`, and 2) `response_data["data"][0]["interfaces"][1]["name"]` evaluates to `"GigabitEthernet2"`.
The top-level structure is a dictionary containing the key 'data', which holds a list of device objects. Index 0 retrieves the first device object, where key 'host_name' yields 'Edge-Router-1'. Within that device object, key 'interfaces' yields a list of interfaces where index 1 selects the second interface object, yielding the name 'GigabitEthernet2'.

Adım Adım Çözüm

1
Examine the outer JSON structure
The root structure is a JSON object (Python dictionary). The top-level key `"data"` maps to a JSON array (Python list) containing a single device dictionary at index 0.
Top-level keys are accessed via dictionary keys, while array values require integer indexing.
2
Evaluate host_name extraction
`response_data["data"][0]["host_name"]` accesses index 0 of `"data"` and retrieves `"Edge-Router-1"`.
Arrays in Python are 0-indexed; therefore index 0 accesses the first element.
3
Evaluate interfaces extraction
`response_data["data"][0]["interfaces"]` is an array of interface objects. Index 0 corresponds to `GigabitEthernet1` and index 1 corresponds to `GigabitEthernet2`.
Index 1 correctly references the second interface element in the list.

Anahtar Kavram

JSON Array Indexing and Nested Dictionary Key Lookup
Bu soruyu puanla