Imagine you are building a REST API that many developers will use. Why is it important to keep the response format consistent across all endpoints?
Think about how you feel when you use tools that behave the same way every time.
Consistent formats reduce confusion and errors. Developers can reuse code and understand responses quickly, improving usability.
Given this API response format, what will be the value of data['user']['name']?
response = {
"status": "success",
"data": {
"user": {
"id": 101,
"name": "Alice",
"email": "alice@example.com"
}
}
}
print(response['data']['user']['name'])Look carefully at the nested keys to find the name.
The key data contains user, which has a name key with value "Alice".
This API sometimes returns a list and sometimes a dictionary for the same endpoint. What error will this cause in client code expecting a consistent format?
response1 = {"items": [{"id": 1}, {"id": 2}]}
response2 = {"items": {"id": 1}}
# Client code expects a list:
for item in response1['items']:
print(item['id'])
for item in response2['items']:
print(item['id'])Check the type of response2['items'] and how the loop treats it.
The second response returns a dictionary instead of a list, so the for-loop tries to iterate over a dict, causing a TypeError.
Choose the option that correctly represents a consistent JSON error response with a message and code.
Remember JSON keys and string values must be in double quotes and use colons.
Option D uses correct JSON syntax with keys and string values in double quotes and colons separating keys and values.
You receive two paginated API responses with consistent formats. How many total items are there combined?
response_page1 = {"status": "success", "data": {"items": [1, 2, 3], "page": 1}}
response_page2 = {"status": "success", "data": {"items": [4, 5], "page": 2}}
combined_items = response_page1['data']['items'] + response_page2['data']['items']
print(len(combined_items))Count the total number of items in both lists combined.
Both pages have lists of items. Adding them combines all items into one list of length 5.