Skip to main content
The TOON JSON Converter provides a Python API for programmatic conversion between TOON and JSON formats. This is useful when you need to integrate TOON conversion into your own applications or scripts.

Installation

The library is a single Python file with no external dependencies beyond Python’s standard library:
import toon_json_converter
Or import specific classes:
from toon_json_converter import (
    BidirectionalConverter,
    TOONEncoder,
    TOONParser,
    EncodeOptions,
    DecodeOptions,
    Delimiter
)

Quick Start

Simple Conversion

The easiest way to convert files is using the BidirectionalConverter class:
from toon_json_converter import BidirectionalConverter

converter = BidirectionalConverter()

# Automatically detects conversion direction from file extension
converter.convert_file("data.json", "data.toon")  # JSON → TOON
converter.convert_file("data.toon", "data.json")  # TOON → JSON

Encoding JSON to TOON

For direct encoding of Python objects to TOON format:
from toon_json_converter import TOONEncoder

encoder = TOONEncoder()
data = {"name": "Alice", "age": 30, "active": True}
toon_string = encoder.encode(data)
print(toon_string)
Output:
name: Alice
age: 30
active: true

Decoding TOON to Python

For parsing TOON format into Python objects:
from toon_json_converter import TOONParser

parser = TOONParser()
toon_content = """
name: Alice
age: 30
active: true
"""
data = parser.parse(toon_content)
print(data)
# {'name': 'Alice', 'age': 30, 'active': True}

Core Components

The library provides three main classes: And two configuration classes:

Working with Options

from toon_json_converter import (
    BidirectionalConverter,
    EncodeOptions,
    DecodeOptions,
    Delimiter
)

# Configure encoding options
encode_opts = EncodeOptions(
    indent_size=4,
    delimiter=Delimiter.TAB,
    length_marker=True,
    key_folding=True
)

# Configure decoding options
decode_opts = DecodeOptions(
    pretty=True,
    indent=4,
    expand_paths=True
)

# Create converter with custom options
converter = BidirectionalConverter(
    encode_options=encode_opts,
    decode_options=decode_opts
)

converter.convert_file("input.json", "output.toon")

Next Steps

Build docs developers (and LLMs) love