A Python project that fetches user data from a real JSON API and performs useful operations such as searching users, creating company summaries, and finding the user with the longest name.
Build a practical Python application that connects to an external API and processes the returned JSON data.
The project demonstrates:
API โ JSON โ Python โ Data Processing โ Analysis
- ๐ Fetch data from a JSON API
- ๐ Search users by name
- ๐ข Create company-wise user summaries
- ๐ Find the user with the longest name
- ๐ Process JSON data using Python
โ ๏ธ Handle API request errors- ๐ Handle network failures
- โฑ๏ธ Use request timeout
- ๐ง Work with nested JSON data
- Python
- REST API
- JSON
requests- Object-Oriented Programming
- Lists
- Dictionaries
- Lambda functions
python-json-api-analyzer/
โ
โโโ main.py
โโโ api_analyzer.py
โโโ README.md
โโโ .gitignore
test_api.py was used during development to understand the API response and should not be uploaded to the final repository.
The project uses:
https://jsonplaceholder.typicode.com/users
JSONPlaceholder provides sample JSON data that can be used for learning and testing APIs.
pip install requestspython main.py๐ JSON API DATA ANALYZER
1. Search User
2. Company Summary
3. Longest Name
4. Exit
The application follows this flow:
Python Program
โ
API Request
โ
JSON API Response
โ
response.json()
โ
Python List / Dictionaries
โ
Data Processing
โ
Analysis / Output
The program sends a GET request:
response = requests.get(
"https://jsonplaceholder.typicode.com/users",
timeout=10
)Sends a GET request to the API.
Prevents the program from waiting indefinitely for the server.
The API response is converted into Python data using:
data = response.json()The returned data is mainly a list containing dictionaries.
For example:
data[0]["name"]gets the first user's name.
For nested data:
data[0]["company"]["name"]gets the first user's company name.
The application searches users by name:
if name.lower() in user["name"].lower():Using .lower() makes the search case-insensitive.
For example:
Search: lean
can find:
Leanne Graham
The project creates a dictionary to count users by company:
summary = {}For each user:
company = user["company"]["name"]If the company is not already present:
if company not in summary:
summary[company] = 0Then its count is increased:
summary[company] += 1This produces a summary such as:
Romaguera-Crona: 1 user(s)
Deckow-Crist: 1 user(s)
Romaguera-Jacobson: 1 user(s)
The project uses Python's max() with a lambda function:
user = max(
users,
key=lambda user: len(user["name"])
)The key tells max() what value should be compared.
Here:
len(user["name"])calculates the length of every user's name.
The user with the greatest name length is returned.
The project checks the HTTP status:
if response.status_code != 200:
print("API request failed. โ")
return []A successful API request normally returns:
200
The program also handles request/network errors:
except requests.RequestException:
print("Network error. Please check your internet connection. โ")
return []This prevents the application from crashing when a request fails.
Provides the HTTP status code of the API response.
Example:
200 โ Successful request
404 โ Resource not found
500 โ Server error
Converts JSON response data into Python objects such as lists and dictionaries.
Converts text to lowercase.
Useful for case-insensitive comparisons.
Checks whether one value exists inside another.
Example:
"lean" in "leanne graham"returns:
True
Used to find the maximum item according to a specific value.
Example:
max(users, key=lambda user: len(user["name"]))This pattern:
if company not in summary:
summary[company] = 0
summary[company] += 1is a common Python technique for counting occurrences.
The project handles:
- Failed API requests
- Network errors
- Empty search input
- Invalid menu choices
- API timeout
- Classes
- Objects
- Methods
- Lists
- Dictionaries
- Nested dictionaries
- Loops
- Conditions
- String methods
- Lambda functions
max()- Exception handling
- REST API basics
- GET requests
- HTTP status codes
- JSON responses
- Nested JSON
- API timeout
- Network error handling
requests.get()response.status_coderesponse.json()requests.RequestException
Previous project:
CSV โ Python โ SQLite โ SQL Analysis
Current project:
API โ JSON โ Python โ Data Analysis
This project is an important step toward working with real APIs and eventually building AI applications that consume external data.
Possible upgrades:
- Add more API endpoints
- Search by email
- Search by company
- Search by city
- Display all users
- Add sorting
- Export API data to CSV
- Store API data in SQLite
- Add API caching
- Build a Streamlit interface
- Add charts and visualizations
Ayushman Tiwari
GitHub:
https://github.com/ayushman-ece
Completed โ
This project demonstrates how Python can communicate with an external JSON API, process nested JSON data, and perform useful data analysis with error handling.