Convert Python data structures to/from TOON (Token-Oriented Object Notation) - a lightweight, human-readable format optimized for LLM-friendly structured data.
pip install json-toonTOON is a Token-Oriented Object Notation format that represents structured data in a clean, readable way:
- Nested dictionaries with indentation
- Tables for lists of objects (using
|separators) - Comma-separated lists for simple arrays
- Human-readable and LLM-friendly
from json_toon import json_to_toon
data = {
"name": "Alice",
"age": 30,
"items": ["apple", "banana", "cherry"]
}
toon_string = json_to_toon(data)
print(toon_string)
# Output:
# name: Alice
# age: 30
# items:
# apple, banana, cherryfrom json_toon import toon_to_json
toon_text = """
name: Alice
age: 30
items:
apple, banana, cherry
"""
data = toon_to_json(toon_text)
print(data)
# Output: {'name': 'Alice', 'age': 30, 'items': ['apple', 'banana', 'cherry']}from json_toon import to_json_str
toon_text = """
name: Alice
age: 30
"""
json_string = to_json_str(toon_text)
print(json_string)
# Output: Pretty-printed JSON stringfrom json_toon import json_to_toon
users = [
{"name": "Alice", "age": 30, "city": "NYC"},
{"name": "Bob", "age": 25, "city": "LA"}
]
toon_string = json_to_toon(users)
print(toon_string)
# Output:
# name | age | city
# Alice | 30 | NYC
# Bob | 25 | LAConvert Python object (dict, list, primitives) to TOON format string.
Parameters:
data: Python object to convertindent: Starting indentation level (default: 0)
Returns: TOON-formatted string
Parse TOON-formatted text back into Python object.
Parameters:
toon_text: TOON format string
Returns: Python object (dict, list, or primitive)
Convert TOON text directly to JSON string.
Parameters:
toon_text: TOON format string
Returns: Pretty-printed JSON string
To install in development mode:
pip install -e .MIT License - See LICENSE file for details.