A Python CLI tool that fetches data from a REST API and saves to JSON with automation support.
JSONPlaceholder - https://jsonplaceholder.typicode.com
- Fetches data from JSONPlaceholder API
- Saves data to JSON file
- Handles network errors (connection, timeout, HTTP errors)
- Handles empty response errors
- Logging to both console and file
- CLI arguments for flexible usage
- Safe re-runs with timestamp option
python -m venv venv
venv\Scripts\activate # Windows
pip install -r requirements.txt# Basic usage (fetches posts)
python main.py
# Fetch specific endpoint
python main.py users
python main.py comments
python main.py todos
# Limit results
python main.py posts --limit 5
# Custom output file
python main.py users -o users_data.json
# Safe re-runs (appends timestamp to filename)
python main.py posts --timestamp
# Verbose logging
python main.py posts -v
# Combined options
python main.py todos -l 10 -o my_todos.json -t -vposts(default)commentsalbumsphotostodosusers
| Option | Description |
|---|---|
endpoint |
API endpoint to fetch (default: posts) |
-o, --output |
Output filename (default: data.json) |
-l, --limit |
Limit number of results |
-t, --timestamp |
Append timestamp to filename for safe re-runs |
-v, --verbose |
Enable debug logging |
- Project exists
- API used: JSONPlaceholder
- Saves data to JSON
- Handles network / empty-response errors
- Logging added
- API → file pipeline working
- Script can be re-run without breaking (use
--timestamp)
- Used command-line arguments
- Script behavior changes based on CLI input