-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_schema_md.py
More file actions
55 lines (41 loc) · 1.36 KB
/
generate_schema_md.py
File metadata and controls
55 lines (41 loc) · 1.36 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
import json
import sys
from pathlib import Path
def explain_json_schema(obj, indent=0):
spacing = " " * indent
explanation = ""
if isinstance(obj, dict):
explanation += f"{spacing}Object {{\n"
for key, value in obj.items():
explanation += f"{spacing} '{key}': {explain_json_schema(value, indent + 1)}\n"
explanation += f"{spacing}}}"
elif isinstance(obj, list):
if len(obj) == 0:
explanation += "Array[]"
else:
types_in_array = {type(item).__name__ for item in obj}
type_str = ", ".join(types_in_array)
explanation += f"Array[{type_str}] (example: {explain_json_schema(obj[0], indent + 1)})"
else:
explanation += type(obj).__name__
return explanation
def main():
if len(sys.argv) < 2:
print("Usage: python generate_schema_md.py <path_to_json_file>")
sys.exit(1)
json_path = Path(sys.argv[1])
if not json_path.is_file():
print(f"Error: File not found: {json_path}")
sys.exit(1)
with open(json_path, "r", encoding="utf-8") as f:
data = json.load(f)
md_content = f"""
- [{json_path.name}]({json_path.name})
??? info "JSON Schema: {json_path.name}"
```text
{explain_json_schema(data, indent=8)}
```
"""
print(md_content)
if __name__ == "__main__":
main()