Computer Science: APIs and JSON Data Exchange
Understanding requests, responses, endpoints, and structured data
Computer Science: APIs and JSON Data Exchange
Understanding requests, responses, endpoints, and structured data
Computer Science - Grade 9-12
- 1
In your own words, explain what an API is and why a weather app might use one.
Think of an API as a structured way for programs to communicate.
An API is a set of rules that lets one software program request data or services from another program. A weather app might use an API to request current temperature, forecasts, and alerts from a weather data service. - 2
A music app sends a request to this endpoint: https://api.example.com/songs/42. What resource is the app most likely requesting?
The app is most likely requesting information about the song with the ID number 42. - 3
Match each HTTP method to its usual purpose: GET, POST, PUT, DELETE. Describe what each one is commonly used for in a REST API.
Focus on the basic CRUD actions: create, read, update, and delete.
GET is commonly used to read or retrieve data. POST is commonly used to create new data. PUT is commonly used to replace or update existing data. DELETE is commonly used to remove data. - 4
A client sends a GET request to /users/17 and receives status code 200. What does the 200 status code mean in this situation?
The status code 200 means the request was successful, so the server found the user data and returned a valid response. - 5
Look at this JSON object: {"id": 8, "name": "Maya", "isAdmin": false, "courses": ["CS", "Math"]}. Identify one string value, one number value, one Boolean value, and one array.
JSON data types include strings, numbers, Booleans, arrays, objects, and null.
The string value is "Maya". The number value is 8. The Boolean value is false. The array is ["CS", "Math"]. - 6
Write a valid JSON object for a book with these fields: title is The Hobbit, author is J.R.R. Tolkien, year is 1937, and available is true.
A valid JSON object is {"title":"The Hobbit","author":"J.R.R. Tolkien","year":1937,"available":true}. The property names and string values are in double quotes, while the number and Boolean are not. - 7
Find and correct the error in this JSON: {name: "Ava", "score": 95, "passed": true}
In JSON, all object keys must use double quotes.
The corrected JSON is {"name":"Ava","score":95,"passed":true}. The error is that the key name must be inside double quotes. - 8
A server returns this response: {"city":"Phoenix","temperatureF":104,"conditions":"Sunny"}. Write a sentence that a weather app could display to a user using this data.
A weather app could display: The weather in Phoenix is sunny with a temperature of 104 degrees Fahrenheit. - 9
A student says, "JSON and JavaScript objects are always exactly the same." Explain why this statement is not fully correct.
JSON is based on JavaScript object syntax, but it is more limited and language independent.
The statement is not fully correct because JSON is a text data format with strict rules, such as requiring double quotes around keys and string values. JavaScript objects can include features that JSON cannot, such as functions, undefined values, and unquoted property names. - 10
An API documentation page says: GET /products?category=shoes&maxPrice=60. Identify the endpoint path and the two query parameters.
The endpoint path is /products. The two query parameters are category with the value shoes and maxPrice with the value 60. - 11
A client sends login information to an API. Should the request body use GET or POST? Explain your choice.
GET requests often put data in the URL, while POST requests can send data in the body.
The request should use POST because login information is being sent to the server for processing. POST is more appropriate for sending data in the request body, and sensitive information should not be placed in a URL query string. - 12
Read this JSON array: [{"username":"lee","points":120},{"username":"sam","points":175},{"username":"nia","points":150}]. Which user has the highest number of points, and how many points do they have?
Sam has the highest number of points with 175 points. - 13
Draw or describe the flow of data when a web page requests movie data from an API and displays it to the user. Include the client, request, server, response, and JSON data.
Start with the browser or app, then show the request going to the server and the response coming back.
The web page acts as the client and sends an HTTP request to the API server. The server processes the request and sends an HTTP response containing JSON movie data. The client parses the JSON and displays the movie information to the user. - 14
An API returns status code 404 for GET /students/999. What does this most likely mean, and how should the app respond?
The 404 status code most likely means the requested student resource was not found. The app should show a clear message such as "Student not found" instead of crashing or displaying confusing data. - 15
Design a JSON response for an API endpoint that returns information about a school club. Include the club name, meeting day, room number, and a list of at least three member names.
Use an array for the list of members.
One valid response is {"clubName":"Robotics Club","meetingDay":"Wednesday","roomNumber":214,"members":["Ari","Jasmine","Noah"]}. This is valid JSON because it uses quoted keys, correct data types, and an array for the member list.