-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
84 lines (63 loc) · 2.21 KB
/
Copy pathparse.py
File metadata and controls
84 lines (63 loc) · 2.21 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
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
"""
Compare the key structure of two JSON files (e.g. locale files) and report
which keys exist in one file but not the other.
Usage:
python compare_json_keys.py file_a.json file_b.json
"""
import json
import sys
def flatten_keys(data, prefix=""):
"""Recursively collect all dotted key paths from a nested dict."""
keys = set()
if isinstance(data, dict):
for key, value in data.items():
path = f"{prefix}.{key}" if prefix else key
if isinstance(value, dict):
keys |= flatten_keys(value, path)
else:
keys.add(path)
else:
# Non-dict at top level, just add the prefix itself
keys.add(prefix)
return keys
def load_json(path):
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
print(f"Error: file not found: {path}")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"Error: invalid JSON in {path}: {e}")
sys.exit(1)
def main():
if len(sys.argv) != 3:
print(f"Usage: python {sys.argv[0]} <file_a.json> <file_b.json>")
sys.exit(1)
file_a, file_b = sys.argv[1], sys.argv[2]
data_a = load_json(file_a)
data_b = load_json(file_b)
keys_a = flatten_keys(data_a)
keys_b = flatten_keys(data_b)
only_in_a = sorted(keys_a - keys_b)
only_in_b = sorted(keys_b - keys_a)
print(f"Comparing:\n A = {file_a}\n B = {file_b}\n")
if not only_in_a and not only_in_b:
print("✅ Both files have identical key structures.")
return
if only_in_a:
print(f"❌ Keys missing in {file_b} (present in {file_a}): {len(only_in_a)}")
for key in only_in_a:
print(f" - {key}")
print()
if only_in_b:
print(f"❌ Keys missing in {file_a} (present in {file_b}): {len(only_in_b)}")
for key in only_in_b:
print(f" - {key}")
print()
total_a = len(keys_a)
total_b = len(keys_b)
print(f"Total keys: {file_a} = {total_a}, {file_b} = {total_b}")
if __name__ == "__main__":
main()