1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import json
def json_to_ts_interface(json_str: str, interface_name: str = "RootObject") -> str:
data = json.loads(json_str)
lines = [f"export interface {interface_name} {{"]
for key, value in data.items():
ts_type = "string"
if isinstance(value, bool):
ts_type = "boolean"
elif isinstance(value, (int, float)):
ts_type = "number"
elif isinstance(value, list):
ts_type = "any[]"
elif isinstance(value, dict):
ts_type = "Record<string, any>"
lines.append(f" {key}: {ts_type};")
lines.append("}")
return "\n".join(lines)
payload = '{"id": 1, "name": "Alex", "isActive": true}'
print(json_to_ts_interface(payload))