Question

Difficulty: MediumInterpreting JSON Encoded Data

A network administrator executes a RESTCONF GET request to retrieve interface and OSPF routing configuration details from a Cisco router. The response payload is stored in a Python dictionary named `data` as shown below:

{
"ietf-interfaces:interface": {
"name": "GigabitEthernet0/0/1",
"description": "Uplink to Core",
"ietf-ip:ipv4": {
"address": [
{
"ip": "10.10.20.1",
"netmask": "255.255.255.0"
}
]
},
"Cisco-IOS-XE-ospf:ospf": {
"process-id": [
{
"id": 100,
"area": [
{
"area-id": "0.0.0.0",
"cost": 20
}
]
}
]
}
}
}

Match each Python expression on the left to the corresponding value extracted from the JSON structure on the right.

  • data["ietf-interfaces:interface"]["name"]"GigabitEthernet0/0/1"
  • data["ietf-interfaces:interface"]["ietf-ip:ipv4"]["address"][0]["ip"]"10.10.20.1"
  • data["ietf-interfaces:interface"]["Cisco-IOS-XE-ospf:ospf"]["process-id"][0]["id"]100
  • data["ietf-interfaces:interface"]["Cisco-IOS-XE-ospf:ospf"]["process-id"][0]["area"][0]["cost"]20

Answer

Matching pairs: data["ietf-interfaces:interface"]["name"] maps to "GigabitEthernet0/0/1"; data["ietf-interfaces:interface"]["ietf-ip:ipv4"]["address"][0]["ip"] maps to "10.10.20.1"; data["ietf-interfaces:interface"]["Cisco-IOS-XE-ospf:ospf"]["process-id"][0]["id"] maps to 100; data["ietf-interfaces:interface"]["Cisco-IOS-XE-ospf:ospf"]["process-id"][0]["area"][0]["cost"] maps to 20.
In Python, JSON objects deserialize into dictionaries accessed via string keys in square brackets, while JSON arrays deserialize into lists accessed using 0-based integer indices. Traversal through keys and list indices accurately extracts specified values.

Step-by-Step Solution

1
Identify JSON data types (objects vs. arrays)
Curly braces {} indicate dictionaries requiring key strings, while square brackets [] indicate lists requiring 0-based integer indexing.
Correctly distinguishing objects from arrays is necessary to apply the proper syntax when parsing Python data structures.
2
Traverse the data path for each expression
Evaluate index positions (0 for the first item in arrays) and dictionary key names sequentially to reach targeted leaf nodes.
Ensures accurate extraction of both string and numeric data values from deeply nested structures.

Key Concept

Interpreting JSON Encoded Data Structures in Python
Rate this question