300-435 · Question #146
Refer to the exhibit. A network engineer must configure a loopback on all routers. To automate the procedure, a script is used. Which code snippet must be added to the box in the code to perform…
The correct answer is A. json.dumps(payload). When sending a Python dictionary as a JSON body in an HTTP request, it must first be serialized to a JSON-formatted string using json.dumps().
Question
Exhibit
Options
- Ajson.dumps(payload)
- Bjson.loads(payload)
- Cjson.dump(payload)
- Djson.load(payload)
How the community answered
(29 responses)- A93% (27)
- B3% (1)
- D3% (1)
Why each option
When sending a Python dictionary as a JSON body in an HTTP request, it must first be serialized to a JSON-formatted string using json.dumps().
json.dumps() serializes a Python object (such as a dict) into a JSON string, which is required when passing data as the body of an HTTP request. Without this conversion, the requests library would send a raw Python dict representation instead of valid JSON. The other variants either read JSON rather than write it, or write to a file object rather than returning a string.
json.loads() deserializes a JSON string into a Python object - it reads JSON, it does not produce it for use in a request body.
json.dump() writes a Python object as JSON directly to a file-like object, not to a string variable usable as a request payload.
json.load() reads JSON from a file-like object and returns a Python object - it is the file-based counterpart to json.loads(), not a serializer.
Concept tested: Python json.dumps for HTTP request serialization
Source: https://docs.python.org/3/library/json.html
Topics
Community Discussion
No community discussion yet for this question.
