nerdexam
Cisco

350-401 · Question #443

Which line must be added in the Python function to return the JSON object {"cat_9k": "FXS1932Q2SE")?

The correct answer is A. return (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']})). To transform a JSON string into a specific JSON object, the Python function must parse the input, extract the required key-value pairs using a dictionary comprehension, and then serialize the resulting dictionary back to a JSON string.

Submitted by krish.m· Mar 6, 2026None

Question

Which line must be added in the Python function to return the JSON object {"cat_9k":

"FXS1932Q2SE")?

Exhibits

350-401 question #443 exhibit 1
350-401 question #443 exhibit 2

Options

  • Areturn (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']}))
  • Breturn (json.dumps({for d in json.loads(test_json)['response']: d['hostname']: d['serialNumber']}))
  • Creturn (json.loads({d['hostname']: d['serialNumber'] for d in json.dumps(test_json)['response'}))
  • Dreturn (json.loads({for d in json.dumps(test_json)['response']: d['hostname']: d['serialNumber']}))

How the community answered

(18 responses)
  • A
    78% (14)
  • B
    6% (1)
  • C
    11% (2)
  • D
    6% (1)

Why each option

To transform a JSON string into a specific JSON object, the Python function must parse the input, extract the required key-value pairs using a dictionary comprehension, and then serialize the resulting dictionary back to a JSON string.

Areturn (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']}))Correct

This line correctly uses `json.loads(test_json)` to parse the input JSON string, then a dictionary comprehension `{{d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']}}` to create the desired key-value pair, and finally `json.dumps()` to convert the Python dictionary into the required JSON string output.

Breturn (json.dumps({for d in json.loads(test_json)['response']: d['hostname']: d['serialNumber']}))

This choice contains a syntax error in the dictionary comprehension; the key-value pair must precede the `for` loop, i.e., `{{key: value for item in iterable}}`.

Creturn (json.loads({d['hostname']: d['serialNumber'] for d in json.dumps(test_json)['response'}))

This choice incorrectly uses `json.dumps(test_json)` (attempting to serialize an already serialized string) and then `json.loads()` at the end, which would parse a JSON string, not serialize a Python dictionary to JSON.

Dreturn (json.loads({for d in json.dumps(test_json)['response']: d['hostname']: d['serialNumber']}))

This choice combines a syntax error in the dictionary comprehension (like B) with the incorrect use of `json.loads()` for the final output (like C).

Concept tested: Python JSON parsing and dictionary comprehension

Source: https://docs.python.org/3/library/json.html

Topics

#Python#JSON parsing#JSON serialization#Dictionary comprehension

Community Discussion

No community discussion yet for this question.

Full 350-401 Practice