Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
68 lines (61 loc) · 2 KB
/
Copy pathexample.py
File metadata and controls
68 lines (61 loc) · 2 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
import njft
import json
# 1. Define a complex, nested JSON object as a Python dictionary.
# This represents the kind of complex data structure you might get from an API.
complex_data = {
"project_id": "P-487A",
"project_name": "Q4 Website Relaunch",
"is_active": True,
"budget_usd": 250000.50,
"manager": {
"user_id": 101,
"full_name": "Elias Vance",
"role": "Lead Product Manager",
"contact": {
"email": "elias.vance@corp.com",
"phone": None
}
},
"milestones": [
{
"name": "Phase 1: Discovery",
"status": "Completed",
"due_date": "2025-10-15",
"tasks": [
{"task_id": 1, "description": "User persona creation", "completed": True},
{"task_id": 2, "description": "Competitor analysis", "completed": True}
]
},
{
"name": "Phase 2: Development",
"status": "In Progress",
"due_date": "2025-12-01",
"tasks": [
{"task_id": 3, "description": "Backend API integration", "completed": False}
]
}
],
"dependencies": ["Marketing Assets", "Legal Review"],
"settings": {
"version": 2.1,
"is_public": False
}
}
# Convert the Python dictionary to a JSON string for the library.
# The `indent=2` is just for pretty-printing the input.
input_json_string = json.dumps(complex_data, indent=2)
print("="*80)
print("🚀 CONVERTING COMPLEX JSON TO TOKEN-EFFICIENT TOON FORMAT 🚀")
print("="*80)
# 2. Display the original complex JSON.
print("\n--- 📄 Original Complex JSON ({} characters) ---".format(len(input_json_string)))
print(input_json_string)
# 3. Use the njft.encode() function to convert the JSON string.
# This is the core functionality of the library.
toon_output = njft.encode(input_json_string)
print("\n\n--- ✨ Simplified TOON Output ({} characters) ---".format(len(toon_output)))
print(toon_output)
print("\n" + "="*80)
reduction = 100 * (len(input_json_string) - len(toon_output)) / len(input_json_string)
print(f"✅ Character count reduced by {reduction:.2f}%")
print("="*80)