Repository navigation
Expand file tree
/
Copy pathparse_json.py
More file actions
71 lines (56 loc) · 2.1 KB
/
Copy pathparse_json.py
File metadata and controls
71 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""
parse_json.py
------------------
Fetches JSON from a URL (or reads a local JSON file) and demonstrates
parsing/navigating it: accessing keys, iterating lists, and handling
missing fields safely.
Usage:
python3 parse_json.py --url https://jsonplaceholder.typicode.com/users
python3 parse_json.py --file data.json
"""
import argparse
import json
import requests
def get_json_data(url=None, filepath=None):
if url:
response = requests.get(url, timeout=15)
response.raise_for_status()
return response.json()
elif filepath:
with open(filepath, "r") as f:
return json.load(f)
else:
raise ValueError("Either url or filepath must be provided")
def explore_json(data):
print(f"Top-level type: {type(data).__name__}\n")
if isinstance(data, list):
print(f"List with {len(data)} item(s). Showing first item:")
if data:
print(json.dumps(data[0], indent=2)[:800])
print(f"\nExample - safely accessing a field from item 0:")
first = data[0]
if isinstance(first, dict):
# .get() avoids KeyError if the field doesn't exist
print(f" name/title: {first.get('name', first.get('title', 'N/A'))}")
elif isinstance(data, dict):
print("Top-level keys:", list(data.keys()))
print("\nFull structure (first 800 chars):")
print(json.dumps(data, indent=2)[:800])
else:
print("Value:", data)
def main():
parser = argparse.ArgumentParser(description="Fetch and parse a JSON response")
parser.add_argument("--url", help="URL returning JSON")
parser.add_argument("--file", help="Local JSON file path")
args = parser.parse_args()
if not args.url and not args.file:
print("Provide either --url or --file")
return
try:
data = get_json_data(url=args.url, filepath=args.file)
explore_json(data)
except (requests.RequestException, json.JSONDecodeError, FileNotFoundError) as e:
print(f"Failed to fetch/parse JSON: {e}")
if __name__ == "__main__":
main()