Which line must be added in the Python function to return the JSON object {`cat_9k`: `FXS1932Q2SE`}?
Which line must be added in the Python function to return the JSON object {`cat_9k`: `FXS1932Q2SE`}?
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.
I might as well go take a application programming course at this point.
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...
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())
json.loads() takes in a string and returns a json object. json. dumps() takes in a json object and returns a string.
A (dictionary comprehension over the JSON contents)
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']
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.
all options except A give syntax error
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
json.loads function for string to object. json.dumps function for object to string. question for return object. Keyword = dumps D for D.
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())
answer is A tested, it works as expected thats a python list comprehension (the list has only one element which is the main dictionary)
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"
A is correct
d for d
I didn't think A or C could be correct because of the position of the 'for d in'
Tested it. A is the correct one that gets the requested output
D is the correct answer ... If you have a JSON string, you can parse it by using the json.loads() method.
This is B