Skip to main content

Bidirectional converter between JSON and TOON (Token-Oriented Object Notation) format with full spec support

Project description

json2toon

PyPI version Python Version CI License: MIT Downloads Code style: ruff

A Python library and CLI tool for bidirectional conversion between JSON and TOON (Token-Oriented Object Notation) format with full specification support.

What is TOON?

TOON (Token-Oriented Object Notation) is a compact, human-readable serialization format designed specifically for passing structured data to Large Language Models (LLMs). It achieves 30-60% fewer tokens than formatted JSON on large uniform arrays while maintaining lossless conversion.

Key features of TOON:

  • CSV-like compactness with explicit structure
  • Indentation-based syntax (like YAML)
  • Tabular formatting for uniform data
  • Optimized for LLM token efficiency

Learn more: TOON Format Specification

Features

  • Full TOON Spec Support: Complete implementation of the TOON specification
  • Bidirectional Conversion: Convert JSON to TOON and TOON back to JSON
  • Tabular Array Detection: Automatically detects and formats uniform arrays efficiently
  • Custom Delimiters: Support for comma, tab, and pipe delimiters
  • Key Folding: Optional collapsing of single-key object chains
  • Path Expansion: Optional expansion of dotted keys into nested objects
  • Strict Mode: Validation for array counts, indentation, and structure
  • CLI Tools: Command-line utilities for easy conversion
  • Type Safe: Full type hints for better IDE support

Installation

Install using uv:

uv add json2toon

Or using pip:

pip install json2toon

Quick Start

Python API

from json2toon import json_to_toon, toon_to_json

# Convert JSON to TOON
data = {
    "users": [
        {"id": 1, "name": "Alice", "role": "admin"},
        {"id": 2, "name": "Bob", "role": "user"}
    ]
}

toon_string = json_to_toon(data)
print(toon_string)
# Output:
# users[2]{id,name,role}:
#   1,Alice,admin
#   2,Bob,user

# Convert TOON back to JSON
json_data = toon_to_json(toon_string)
print(json_data)  # Original data restored

CLI Usage

Convert JSON to TOON:

# From file
json2toon input.json -o output.toon

# From stdin
cat data.json | json2toon

# With options
json2toon input.json --indent 4 --delimiter tab

Convert TOON to JSON:

# From file
toon2json input.toon -o output.json

# From stdin
cat data.toon | toon2json

# With pretty formatting
toon2json input.toon --pretty

Advanced Usage

Configuration Options

from json2toon import json_to_toon, ToonConfig

config = ToonConfig(
    indent_size=4,              # Indentation spaces (default: 2)
    delimiter="\t",             # Delimiter: ",", "\t", or "|" (default: ",")
    key_folding="safe",         # Collapse single-key chains (default: None)
    strict=True,                # Enable strict validation (default: True)
)

toon_string = json_to_toon(data, config=config)

Parsing Options

from json2toon import toon_to_json, ToonParseConfig

config = ToonParseConfig(
    expand_paths="safe",        # Expand dotted keys (default: None)
    strict=True,                # Strict mode validation (default: True)
)

json_data = toon_to_json(toon_string, config=config)

Examples

Simple Object

JSON:

{
  "id": 123,
  "name": "Ada"
}

TOON:

id: 123
name: Ada

Nested Object

JSON:

{
  "user": {
    "id": 1,
    "name": "Bob"
  }
}

TOON:

user:
  id: 1
  name: Bob

Tabular Arrays (Most Efficient)

JSON:

{
  "products": [
    {"id": 1, "name": "Laptop", "price": 999.99},
    {"id": 2, "name": "Mouse", "price": 29.99}
  ]
}

TOON:

products[2]{id,name,price}:
  1,Laptop,999.99
  2,Mouse,29.99

Primitive Arrays

JSON:

{
  "tags": ["admin", "ops", "dev"]
}

TOON:

tags[3]: admin,ops,dev

CLI Options

json2toon

usage: json2toon [-h] [-o OUTPUT] [--indent SIZE] [--delimiter DELIM]
                 [--key-folding MODE] [--no-strict]
                 [input]

Convert JSON to TOON format

positional arguments:
  input                 Input JSON file (default: stdin)

options:
  -h, --help            Show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output TOON file (default: stdout)
  --indent SIZE         Indentation size (default: 2)
  --delimiter DELIM     Delimiter: comma, tab, or pipe (default: comma)
  --key-folding MODE    Key folding mode: safe (default: none)
  --no-strict           Disable strict mode validation

toon2json

usage: toon2json [-h] [-o OUTPUT] [--expand-paths MODE] [--no-strict]
                 [--pretty]
                 [input]

Convert TOON to JSON format

positional arguments:
  input                 Input TOON file (default: stdin)

options:
  -h, --help            Show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output JSON file (default: stdout)
  --expand-paths MODE   Path expansion mode: safe (default: none)
  --no-strict           Disable strict mode validation
  --pretty              Pretty-print JSON output

Development

Install development dependencies:

uv sync --dev

Run tests:

uv run pytest

Run type checking:

uv run mypy src/json2toon

Run linting:

uv run ruff check src/json2toon

Format code:

uv run ruff format src/json2toon

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • TOON Format Specification - The official TOON format specification
  • Thanks to the TOON format creators for designing an efficient LLM-optimized format

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json2toon-0.1.2.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json2toon-0.1.2-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file json2toon-0.1.2.tar.gz.

File metadata

  • Download URL: json2toon-0.1.2.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for json2toon-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6016d78596ee04441c4bb3dd0f57e39ba9164c1c611c496b0410c56d55b90597
MD5 13b3544a1ed1a058c1770d29a85e7284
BLAKE2b-256 3ff53fd93b10245ce9692ba66081e688417ff288061d3ed1cbcde951a8e4eb38

See more details on using hashes here.

File details

Details for the file json2toon-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: json2toon-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for json2toon-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 990ee11fcd52780dfb72d0b5b5024a7257ccac897ea9e81cf4638c556116f9cb
MD5 92fc98abfd4dc256b958a83ce9a639da
BLAKE2b-256 39614c754880d8ed48bd71469296efefb8abb19b17e4aeb0dd5c5457c588024c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page