Python Practice Set (Part 14): Working with APIs and JSON
- Anubhav Somani
- Sep 3
- 2 min read

Powering Your Scripts with Web Data: A Guide to APIs and JSON
Welcome to Part 14 of our Python practice series! We've learned how to work with data on our own computer, but what if we want to use live data from the web? That's where APIs (Application Programming Interfaces) come in. An API is like a menu that a web service provides for us to request data. The data is usually returned in a format called JSON(JavaScript Object Notation), which looks very similar to Python dictionaries and lists.
In this set, you'll learn how to parse JSON and make live API calls to fetch data from the internet, a crucial skill for any developer.
Note: For the API questions, we'll use the popular requests library. If you don't have it installed, you can add it to your environment by running: pip install requests
1. The json Module: From String to Dictionary
Before making live web requests, let's learn to handle JSON data. You have the following JSON data stored as a Python string. Your task is to parse this string into a Python dictionary and then print the user's email address.
Hint: Use the json.loads() method to load a string.
Python
user_json_string = '{"id": 101, "name": "Alice", "email": "alice@example.com"}'
2. Navigating Nested JSON Data
JSON data is often nested. You have a more complex JSON string representing a user profile. Parse this string and access the user's second skill from the skills list.
Python
profile_json_string = '''
{
"username": "coder_dev",
"profile": {
"is_active": true,
"last_login": "2023-10-27"
},
"skills": ["Python", "SQL", "Web APIs"]
}
'''
3. Your First Live API Call with requests
It's time to fetch live data! Write a function get_user_data that takes a user_id as an argument. The function should:
Make a GET request to the JSONPlaceholder API for a specific user (e.g., https://jsonplaceholder.typicode.com/users/1).
Check if the request was successful (a status code of 200).
If successful, parse the JSON response and print the user's name and city.
4. Looping Through a List of API Results
APIs often return a list of items. Write a function get_latest_posts that fetches all the posts from https://jsonplaceholder.typicode.com/posts. Loop through the resulting list and print the title of the first 5 posts.
5. Building a Resilient API Function with Error Handling
Real-world API calls can fail. Write a function fetch_post that takes a post_id. This function must be robust and handle potential errors:
It should make a request to https://jsonplaceholder.typicode.com/posts/{post_id}.
Use a try-except block to catch potential network errors (e.g., requests.exceptions.RequestException).
If the request is successful, check the status code. If it's 200, print the post's title. If it's 404, print a message like "Post with ID [post_id] not found."
Test your function with a valid ID (e.g., 1) and an invalid ID (e.g., 999).
Lucky Link - https://amzn.to/4g3AUfr
Comments