Skip to content

chauhan081/Python-Project-8-Fetch-Process-API-Data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Python Project 8: Fetch & Process API Data πŸŒπŸ“‘

πŸ“Œ Problem Statement:

Write a Python script that fetches live data from a public API, processes it, and saves it to a JSON file.

🧠 What You'll Learn:

  • Making GET requests with Python’s requests module
  • Handling API responses (JSON)
  • Parsing and saving API data

βœ… Python Code:

import requests
import json

# API endpoint (Example: Public API for Random Users)
url = "https://randomuser.me/api/?results=5"

# Fetch data from API
response = requests.get(url)

# Check if request was successful
if response.status_code == 200:
    data = response.json()
    print("API Data Fetched Successfully!\n")

    # Print fetched data
    print(json.dumps(data, indent=4))

    # Process data (Extract only names and emails)
    users = []
    for user in data['results']:
        users.append({
            "name": f"{user['name']['first']} {user['name']['last']}",
            "email": user['email']
        })

# Save processed data to JSON
    with open("users.json", "w") as f:
        json.dump(users, f, indent=4)

    print("\nProcessed user data saved to users.json")

else:
    print(f"Error: Unable to fetch data (Status Code: {response.status_code})")

πŸ“₯ How It Works:

  1. Sends a GET request to a public API.
  2. Parses the JSON response.
  3. Extracts user names & emails.
  4. Saves clean data into a local users.json file.

πŸ’‘ Use Cases:

βœ… Collecting data for dashboards

βœ… Building real-time apps

βœ… Automating daily reports

About

Write a Python script that fetches live data from a public API, processes it, and saves it to a JSON file.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published