python json module requests REST API calls 2024 Interview Q&A
Prepare for senior technical positions. Click on any question to expand and review details.
Here is an interview preparation module based on provided materials on Python JSON and APIs.
Python JSON & APIs: Interview Prep Q&THE
Question 1: What's the fundamental difference between the JSON object and the Python dictionary, and what are the terms used when converting between a two? Answer: To the intermediate Python developer the JSON string might look exactly like a Python dictionary. There is a essential difference. A Python dictionary is the living breathing object stored within a computer's memory. In contrast, a JSON object is just flat text formatted in the very specific way, making it a universal language for transferring data. When you need to send living Python object over the internet you really have to flatten it into text— process called serialization. Conversely, when you receive flat JSON text from API and convert it back into usable Python dictionary this process is basically known as deserialization.
Question 2: Python has really a built-in module to HTTP requests called urllib. Why does a Python community overwhelmingly favor the external requests library instead?
Answer: While urllib comes built into Python, modern programming rarely uses it directly because of its complexity. The external requests library is preferred because it abstracts the ugly complicated parts of web networking behind simple, highly readable commands, while officially subtitled "HTTP for Humans," it is designed specifically to be user-friendly. Because of this accessible design, it has become the undisputed standard, garnering around 300 million downloads per week and serving as dependency to over 4 million GitHub repositories.
Question 3: You have a script that successfully fetches API data using response.json(). Yet, program suddenly crashes into production of JSONDecodeError. Why did this happen. How would a professional refactor the code to handle it?
Answer: This crash usually happens when the API server experiences an issue and returns an HTML error page instead of an expected JSON text, while when the script blindly calls response.json() on HTML, it triggers a JSONDecodeError and halts the program.
A professional ensures safe deserialization by wrapping the request in a robust structure. Instead for just parsing a JSON immediately, you should:
* Explicitly check if a response status is successful.
* Implement timeouts so program doesn't hang indefinitely.
* Wrap the request and parsing logic in try-except blocks to gracefully catch network errors or decoding failures without crashing the entire application.
Question 4: You're basically building an integration to pull data from a strict API, but your requests.get() calls keep getting blocked or rejected. Assuming your URL is correct what's likely causing this, and how can you use Headers for fix it?
Answer: APIs often block default Python scripts to prevent spam bots. By default, the requests library sends User-Agent header that essentially announces to the server, "I am Python script."
To bypass these blocks and prove good citizenship you can pass custom Header in your request. By updating the User-Agent header you give your application a polite custom name. Also, headers can probably be used to pass API tokens to prove you have permission to access data, or to explicitly inform an API that you specifically expect JSON data to be returned.
Question 5: You have written the robust, error-proof script to fetch data from the API, but you now need to make 100 separate API requests. Why is simply looping through requests.get() a poor solution for this and what modern Python feature fix this problem?
Answer: Standard requests.get() calls are synchronous. This means that every time you make request your Python program completely freezes and waits for the server towards reply before moving upon to the next line of code, while if you loop through 100 requests sequentially the total execution time will be extremely slow.
To sort out this massive real-world performance bottleneck, intermediate to advanced developers use Python Async/Await (asyncio). This allows the program towards send out all 100 requests concurrently without freezing, drastically reducing a time it takes to gather the data.