How do you fetch JSON from a REST API and parse it?

This tests practical fluency with HTTP mechanics and JSON deserialization. A strong answer names the method, URL, and headers; checks the status code; then parses with r.json() or json.loads. A red flag is skipping error handling or confusing GET with POST.
WHAT THIS TESTS: The interviewer wants to see that you can reason about the full lifecycle of an HTTP call rather than relying on magic libraries. They are checking for awareness of the request structure, the response structure, and the failure modes that separate production code from a Jupyter notebook snippet. Even at a beginner level, they expect you to mention status codes, headers, and the difference between text and structured data.
A GOOD ANSWER COVERS: First, identify the four key parts of the request: the HTTP method such as GET for retrieval, the URL that points to the endpoint, optional headers like Accept application/json, and query parameters if the API supports filtering. Second, describe the response pipeline: the server returns a status code like 200 OK, response headers including Content-Type, and a body that contains the JSON payload. Third, explain parsing in Python by importing requests, calling requests.get with the URL, checking r.status_code or calling r.raise_for_status(), then converting the body with r.json(). Fourth, mention error handling: wrap the call in a try block to catch connection errors, timeouts, and JSON decode errors.
COMMON WRONG ANSWERS: A major red flag is skipping the status code check and calling r.json() blindly, which explodes on a 404 or 500 response. Another is confusing GET with POST or claiming that parameters belong in the body of a GET request. Some candidates also forget to mention headers entirely or suggest manual string slicing instead of a proper JSON parser. Finally, omitting timeouts or treating the network as always available signals a lack of production experience.
LIKELY FOLLOW-UPS: The interviewer may ask how you would handle pagination for large result sets, how to add authentication via an Authorization header, or how to rate-limit your requests to avoid being blocked. They might also ask what you would do if the response is not valid JSON, or how to stream a very large response instead of loading it entirely into memory.
ONE CONCRETE EXAMPLE: Suppose you want to fetch the latest public events from GitHub. You would write import requests, then set url to https://api.github.com/events, then call r = requests.get(url, timeout=10). Next you would verify r.status_code is 200, then parse the list of events with data = r.json(). The data variable now holds a Python list of dictionaries that you can iterate over. If the server is unreachable, requests raises a ConnectionError; if the body is malformed, r.json() raises a requests.exceptions.JSONDecodeError.
Source: requests.readthedocs.io
Read the original → requests.readthedocs.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.