350-401 · Question #553
An engineer must export the contents of the devices object in JSON format. Which statement must be used?
The correct answer is B. json.dumps(Devices). To convert a Python object into a JSON formatted string, the json.dumps() function from the json module is the correct method to use.
Question
Options
- Ajson.repr(Devices)
- Bjson.dumps(Devices)
- Cjson.prints(Devices)
- Djson.loads(Devices)
How the community answered
(36 responses)- A6% (2)
- B92% (33)
- C3% (1)
Why each option
To convert a Python object into a JSON formatted string, the `json.dumps()` function from the `json` module is the correct method to use.
There is no standard `json.repr()` function in Python's `json` module; `repr()` is a built-in Python function that returns a developer-friendly string representation of an object, not for JSON serialization.
The `json.dumps()` function serializes a Python object (`Devices` in this case) into a JSON formatted string, which is precisely what is needed to export its contents in JSON format.
There is no `json.prints()` function in Python's standard `json` module; `print()` is a built-in Python function for outputting to the console.
The `json.loads()` function performs the inverse operation, deserializing a JSON formatted string into a Python object, which is not the requirement for exporting an object.
Concept tested: Python JSON serialization (json.dumps)
Source: https://docs.python.org/3/library/json.html#json.dumps
Topics
Community Discussion
No community discussion yet for this question.