Exam 350-401 All QuestionsBrowse all questions from this exam
Question 350

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

    Correct Answer: A

    To return the JSON object {'cat_9k': 'FXS1932Q2SE'} from the given JSON string, the correct line to be added is: return (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']})). This line parses the JSON string into a Python dictionary using json.loads, extracts the 'hostname' and 'serialNumber' from each entry in the 'response' list, and creates a new dictionary with 'hostname' as keys and 'serialNumber' as values. Finally, json.dumps converts this dictionary back into a JSON string, which matches the required format.

Discussion
Dre876

I might as well go take a application programming course at this point.

[Removed]

I've been thinking the same thing. Planning on the CCIE after this, I may take some time before even starting to just run through some code academy courses...

Jared28Option: A

Tested as well, A was the only one that functioned. import json def get_data(): test_json = """ { "response": [{ "managementipAddress": "10.10.2.253", "memorySize": "3398345152", "serialNumber": "FXS1932Q2SE", "softwareVersion": "16.3.2", "hostname": "cat_9k" }], "version": "1.0" } """ return (json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']})) print(get_data())

JohnSmithZhaoOption: A

json.loads() takes in a string and returns a json object. json. dumps() takes in a json object and returns a string.

bogdOption: A

A (dictionary comprehension over the JSON contents)

VaZiOption: A

Right answer should be A json.dumps({d['hostname']: d['serialNum'] for d in json.loads(test_json)['response']}) Checked in Python. B,D "for d in " - Invalid Syntax C - Type Error. json.dumps() returns a string, so this syntax is wrong json.dumps(test_json)['response']

lafrankOption: A

In fact, all answers are incorrect if question is taken literally :-) B,C and D all contain syntax errors. While A is syntactically correct, it returns a string and NOT a JSON object as it was stated in the question ! Be careful with print() as it would both print a JSON object and a string object much the same way. Use print(type(get_data()) instead and you would see that the returned value is of type str.

cvndaniOption: A

all options except A give syntax error

CiscoTerminator

I think Cisco wants to crack the whip on Network engineers who are not taking the Devnet Associate exam first before CCNP Encor....cause hey

dueOption: A

json.loads function for string to object. json.dumps function for object to string. question for return object. Keyword = dumps D for D.

yqpmateoOption: A

json.load method parse a valid JSON string and convert it into a Python dictionary. the test_json variable is a JSON string so the answers that use json.dumps(test_json) are incorrect. Out of the other two, answer A is valid. Here is a python code that proves it: import json def get_data(): test_json = """ { "response" : [{ "managementIPAdd":"10.10.10.2", "memorySize" : "2323232", "serialNumber" : "FX2342232WWQ", "softwareVer" : "14.2.3", "hostname" : "cat_9k" }], "vesion":"1.0" } """ for d in json.loads(test_json)['response']: print(json.dumps({d['hostname']: d['serialNumber']})) return ( json.dumps({d['hostname']: d['serialNumber'] for d in json.loads(test_json)['response']})) print(get_data())

LeGloupierOption: A

answer is A tested, it works as expected thats a python list comprehension (the list has only one element which is the main dictionary)

rlilewisOption: A

Tested and verified myself. As cvndani said - only A works. All others either give an error: "for d in" at the start of the { character results in "SyntaxError: invalid syntax" C returns error "TypeError: string indices must be integers"

SeMo0o0oOption: A

A is correct

SeMo0o0o

d for d

danman32Option: B

I didn't think A or C could be correct because of the position of the 'for d in'

Heim_OxOption: A

Tested it. A is the correct one that gets the requested output

Farid77Option: D

D is the correct answer ... If you have a JSON string, you can parse it by using the json.loads() method.

bara_kenOption: B

This is B