Question 1
Topic: Software Development and Design
Skill tested: Extracting selected values from nested JSON with Python dictionaries and lists.
A network inventory service returns JSON for each site. Your function receives the already-decoded Python object below and must return the hostname of the first device whose role is "edge"; if none exists, it must return None. The response can contain several devices, and device order is significant. The function must not assume that a matching device exists or search textually through the serialized JSON. Which implementation meets the requirement?
{"devices": [{"hostname": "sw-1", "role": "access"}, {"hostname": "rtr-2", "role": "edge"}]}
The decoded response is a dictionary whose devices value is a list of dictionaries. Iterating that list preserves order, checking each role finds the first edge device, and returning None after the loop handles the no-match case. The second implementation looks for hostname at the top level, where it is not present. The third treats a list as a dictionary and uses a Boolean expression as an index. The fourth incorrectly decodes an object that is already decoded and would return the first device regardless of role.