Skip to content

About

A Python utility that fetches JSON data from an API and performs user search, company summaries, and data analysis with error handling.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ JSON API Data Analyzer

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.


๐ŸŽฏ Project Goal

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


โœจ Features

  • ๐ŸŒ 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

๐Ÿ› ๏ธ Technologies Used

  • Python
  • REST API
  • JSON
  • requests
  • Object-Oriented Programming
  • Lists
  • Dictionaries
  • Lambda functions

๐Ÿ“ Project Structure

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.


๐ŸŒ API Used

The project uses:

https://jsonplaceholder.typicode.com/users

JSONPlaceholder provides sample JSON data that can be used for learning and testing APIs.


๐Ÿš€ How to Run

1. Install the required package

pip install requests

2. Run the program

python main.py

๐Ÿ–ฅ๏ธ Menu

๐ŸŒ JSON API DATA ANALYZER
1. Search User
2. Company Summary
3. Longest Name
4. Exit

๐Ÿ”„ How the Project Works

The application follows this flow:

Python Program
      โ†“
API Request
      โ†“
JSON API Response
      โ†“
response.json()
      โ†“
Python List / Dictionaries
      โ†“
Data Processing
      โ†“
Analysis / Output

๐Ÿ“ก Getting API Data

The program sends a GET request:

response = requests.get(
    "https://jsonplaceholder.typicode.com/users",
    timeout=10
)

requests.get()

Sends a GET request to the API.

timeout=10

Prevents the program from waiting indefinitely for the server.


๐Ÿ“ฆ Converting JSON

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.


๐Ÿ”Ž Search User

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

๐Ÿข Company Summary

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] = 0

Then its count is increased:

summary[company] += 1

This produces a summary such as:

Romaguera-Crona: 1 user(s)
Deckow-Crist: 1 user(s)
Romaguera-Jacobson: 1 user(s)

๐Ÿ“ Longest Name

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.


โš ๏ธ API Error Handling

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.


๐Ÿง  Important Concepts

response.status_code

Provides the HTTP status code of the API response.

Example:

200 โ†’ Successful request
404 โ†’ Resource not found
500 โ†’ Server error

response.json()

Converts JSON response data into Python objects such as lists and dictionaries.


.lower()

Converts text to lowercase.

Useful for case-insensitive comparisons.


in

Checks whether one value exists inside another.

Example:

"lean" in "leanne graham"

returns:

True

max() + key

Used to find the maximum item according to a specific value.

Example:

max(users, key=lambda user: len(user["name"]))

Dictionary Counting

This pattern:

if company not in summary:
    summary[company] = 0

summary[company] += 1

is a common Python technique for counting occurrences.


๐Ÿ›ก๏ธ Error Handling

The project handles:

  • Failed API requests
  • Network errors
  • Empty search input
  • Invalid menu choices
  • API timeout

๐Ÿ“š Concepts Learned

Python

  • Classes
  • Objects
  • Methods
  • Lists
  • Dictionaries
  • Nested dictionaries
  • Loops
  • Conditions
  • String methods
  • Lambda functions
  • max()
  • Exception handling

API

  • REST API basics
  • GET requests
  • HTTP status codes
  • JSON responses
  • Nested JSON
  • API timeout
  • Network error handling

requests

  • requests.get()
  • response.status_code
  • response.json()
  • requests.RequestException

๐Ÿ”— Connection With Previous Projects

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.


๐Ÿ”ฎ Future Improvements

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

๐Ÿ‘จโ€๐Ÿ’ป Author

Ayushman Tiwari

GitHub:

https://github.com/ayushman-ece


โญ Project Status

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.

About

A Python utility that fetches JSON data from an API and performs user search, company summaries, and data analysis with error handling.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages