Which Python code snippet must be added to the script to save the returned configuration as a JSON-formatted file?
A.
B.
C.
D.
Which Python code snippet must be added to the script to save the returned configuration as a JSON-formatted file?
A.
B.
C.
D.
I've read the for ENCOR we just need to "6.1 Interpret basic Python components and scripts". I wonder why I see alot of high level question about Python here
I am wondering as well
It is really bothering me how many questions there are about programing languages.
Im a bit nervous about this stuff. If these are brain dumps from the actual exam, why is this stuff on here? This is not networking. ENCOR is very misleading.
even the Cisco U course does not teach this level of detail. The course and the exam is a bit of a scam
Same here...
Answer is C, I verified by writing the below program to test: import json import requests url = "http://ip.jsontest.com" response = requests.get(url) print(response) with open("ifaces.json","w") as outfile: outfile.write(response.text)
A: wrong - python error: "argument must be str, not dict" B: wrong - python error: "argument must be str, not Response" C: Correct. file created as .json D: Wrong - python error: "argument must be str, not dict"
A is correct. json.loads() is taken response.text
B is correct. Response is a variable not a file
C Is correct. Tested all answers.
The correct answer is C. C. with open("ifaces.json", "w") as OutFile: OutFile.write(Response.text) Explanation: When you make a request using the requests.get() method, the response object Response contains the server's response in text format. To save this response as a JSON-formatted file, you need to write the text attribute of the response to the file. Option C does this correctly by using OutFile.write(Response.text). Options A and D are incorrect because they attempt to convert the response content to JSON using json.loads() or Response.json(), respectively. However, in this case, the response is already in text format and should be directly written to the file. Option B is incorrect because it attempts to write the entire response object (Response) to the file, which will not be in valid JSON format.
Ok, I think I got it -> C. Explanation as below. A wrong: json.loads() is used to make a python dictionary out of a json string - this is not what we are looking for as we actually should get a json string. B wrong: This will provide the http response, not a text string. E.g. <Response [200]> if all good. C correct: Since APIs return JSON strings by default we only need to write them into a file. Script example: url = "http://ip.jsontest.com" response = requests.get(url=url) print(response.text," ",type(response.text)) Result: {"ip": "77.112.51.84"} <class 'str'> D wrong: outfile.write(response.json()) changes the type of the data from string to dictionary so we're getting an error: TypeError: write() argument must be str, not dict
Should be C. If you just write "response", I believe you will get the "200"
I will go with B
B is correct. https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/
sorry, C is correct.
D is correct
This reference is the key https://requests.readthedocs.io/en/latest/user/quickstart/#json-response-content
It´s C The ( Response ) object obtained from the ( requests.get() ) method contains the JSON-formatted configuration data in its ( text attribute ).
.........................
can someone show me step-by-step how does this works it always confuses me.do we use postman to do this?.
requests.get() returns a "response" object. write() method accepts only string datatypes as arguments, not anything else. A: Wrong: json.loads() method takes a "string" formatted as Json as argument, and returns a dict. Trying to write a 'dict' to a file raises an exception. B: Wrong: Trying to write the whole Response object to a file raises an exception C: Correct: 'text' is an attribute of 'response' which is the response's body as a Json-formatted string. Thus, it can be directly written to the file. D: Wrong: .json() method returns the response's json body to a dictionary, same as json.loads(response.text) does.
Answer is C