diff --git a/Intermediate/13_json_reader.py b/Intermediate/13_json_reader.py new file mode 100644 index 0000000..ade7257 --- /dev/null +++ b/Intermediate/13_json_reader.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# Made By @Ericwasepic127 +# JSON Reader - A simple CLI tool to read and pretty-print JSON files + +import json +import sys +from pathlib import Path +from typing import Any + +def read_json_file(file_path: Path) -> Any | None: + """Read and parse a JSON file.""" + if not file_path.is_file(): + print(f"ERROR: '{file_path}' is not a file or does not exist.", file=sys.stderr) + return None + + try: + with file_path.open(encoding='utf-8') as f: + return json.load(f) + except json.JSONDecodeError as e: + print(f"ERROR: Invalid JSON format in '{file_path}': {e}", file=sys.stderr) + except Exception as e: + print(f"ERROR: Failed to read '{file_path}': {e}", file=sys.stderr) + + return None + +def display_json(data: Any) -> None: + """Display JSON content in a readable format.""" + if isinstance(data, dict): + for key, value in data.items(): + print(f"Name: {key!r} | Value: {value!r}") + elif isinstance(data, list): + for i, item in enumerate(data): + print(f"[{i}]: {item!r}") + else: + # For primitives like string, number, bool + print(data) + +def main() -> None: + print("Welcome to JSON Reader") + print("Type 'exit' to quit\n") + print(f"Current directory: {Path.cwd()}") + + while True: + try: + loc = input("\nEnter path to your JSON file: ").strip() + + if loc.lower() in ("exit", "quit", "q"): + print("Goodbye!") + break + + if not loc: + continue + + json_path = Path(loc).expanduser().resolve() + data = read_json_file(json_path) + + if data is None: + continue + + display_json(data) + + except (KeyboardInterrupt, EOFError): + print("\nGoodbye!") + break + +if __name__ == "__main__": + main() diff --git a/README.md b/README.md index b7cc293..ace0bc0 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ An open-source GitHub repository containing Python project ideas, steps, tips, a This repository is designed to help Python learners at all levels, starting with beginner-friendly projects and gradually progressing to more advanced ones. Each project includes clear instructions and a working code implementation. -**Total Projects:** 44 +**Total Projects:** 45 --- **Total Beginner Projects:** 32 -**Total Intermediate Projects:** 12 +**Total Intermediate Projects:** 13 ## Table of Contents @@ -21,7 +21,7 @@ This repository is designed to help Python learners at all levels, starting with ## Setup -- Ensure have at least Python `3.6` or later (we recommend `3.12` or later) installed on your computer. You can do a quick search and download it from a trusted provider for your platform. +- Ensure you have at least Python `3.6` or later (we recommend `3.12` or later) installed on your computer. You can do a quick search and download it from a trusted provider for your platform. - Ensure you have an IDE or a place where you can code and run the Python interpreter. @@ -78,6 +78,7 @@ This repository is designed to help Python learners at all levels, starting with | [Fractal Creator](#10-fractal-creator) | Intermediate | 6.5/10 | | [File Explorer](#11-file-explorer) | Intermediate | 6.5/10 | | [File Viewer](#12-file-viewer) | Intermediate | 4.5/10 | +| [JSON Reader](#13-json-reader) | Intermediate | 5/10 | ## Beginner Projects @@ -1425,6 +1426,35 @@ These projects are ideal for those with experience in Python. Each project inclu Use the `open` function for file operations. Note that it raises a `PermissionError` when access is denied. Implement try/except blocks to catch this exception and inform the user accordingly. + +### 13. JSON Reader +- **Difficulty**: 5/10 +- **Description**: Prompt the user for a JSON filename and print the contents of the specified file in a readable key-value format. Ensure the program handles errors gracefully to avoid crashes. The program should run in an infinite loop until the user decides to exit. +- **Solution**: [GitHub Repository](https://github.com/Infinitode/Python-Projects/blob/main/Intermediate/13_json_viewer.py) - Original version by [Ericwasepic127](https://github.com/Ericwasepic127/Python-Projects/). +- **Steps**: + 1. Prompt the user for a filename. + 2. Check if the file exists and is a file: If it does not exist, inform the user with a "File Not Found" message. Ensure to check for directories as well. + 3. Attempt to read and parse the file as JSON. If the JSON is invalid, notify the user with an "Invalid JSON Format" message. If a permission error occurs, notify the user with an "Operation Not Permitted" message. + 4. If parsing is successful, iterate through the data: If it's a dictionary, print each Name and Value pair. If it's a list, print each item with its index. + 5. Repeat the process by returning to step 1, unless the user presses Ctrl-C (KeyboardInterrupt, SIGINT) or types "exit" when prompted for the filename. +- **Tips**: +
Tip 1: + + Create a reusable function `read_json()` to handle file validation and JSON parsing separately to streamline the code and enhance readability. + +
+ +
Tip 2: + + Utilize `os.path.isfile` to verify that the file exists and is not a directory, and use `json.load()` inside a try/except block to catch `json.JSONDecodeError`. + +
+ +
Tip 3: + + Use `repr()` or `!r` formatting when printing keys and values to preserve data types, and check if the loaded JSON is a dict before calling `.items()` to support lists as well. + +
> [!NOTE]