Skip to content
tezvyn:

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

Source: requests.readthedocs.ioEasyHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

A major red flag is skipping the status check and trusting r.json() blindly: r.json() parses the body regardless of whether the status is successful and can fail when the body is not valid JSON. Call r.raise_for_status() before trusting the payload. Query parameters normally go in the URL, while request bodies depend on the method and API contract. 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.

What usually comes next

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.

A 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.

Interview question

Which approach correctly handles the full response lifecycle when fetching JSON from a REST API in production using Python's requests?

  • a.Set a request timeout but skip status checks to parse faster
  • b.Use r.text and manually split the string to extract JSON fields
  • c.Verify the status with r.raise_for_status() before parsing with r.json()Correct
  • d.Pass query parameters in the request body and call r.json() directly
Why?

Calling r.raise_for_status() before trusting r.json() raises an HTTPError for unsuccessful responses before application code treats their bodies as successful payloads. r.json() parses the response body independently and can also raise a JSON decoding error when the body is not valid JSON; timeouts and connection errors need separate handling.

Just read this? Test yourself on what you have been reading.

Read the original → requests.readthedocs.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on http — each one lists the topics its interview covers.

See open roles