When you receive a JSON response from an API, what does the structure usually represent?
Think about how data is stored and shared in a simple, readable format.
API responses in JSON format usually contain data organized as keys and values or lists, making it easy to access specific information.
You receive this JSON response from an API:
{"user": {"name": "Alice", "age": 30, "city": "New York"}}
Which piece of information would you access to get the user's city?
Look for the key that matches the information you want.
The key "city" inside the "user" object holds the city name, so accessing user.city gives "New York".
You try to parse this JSON response:
{"status": "success", "data": [1, 2, 3]}
Which mistake would cause an error when trying to access the first item in the data list?
Consider the data type of the 'data' field before accessing it.
If 'data' is a string, trying to access it like a list with data[0] will cause an error. But here, 'data' is a list, so accessing data[0] is valid.
Which of the following is a key difference between XML and JSON API responses?
Think about the size and complexity of these formats.
JSON is usually lighter and easier to parse because it uses a simpler syntax compared to XML's tag-based structure.
An API returns this JSON response:
{"results": [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}]}
After parsing, how many items are in the 'results' list?
Count the number of objects inside the 'results' array.
The 'results' key holds a list with 4 objects, so the count is 4.