Get the FREE Ultimate OpenClaw Setup Guide →

Toon

npx machina-cli add skill hackermanishackerman/claude-skills-vault/toon --openclaw
Files (1)
SKILL.md
6.0 KB

TOON Format Guide

Overview

TOON (Token-Oriented Object Notation) is a compact encoding of JSON designed for LLM input. Combines YAML-style indentation w/ CSV-style tables for uniform arrays.

Key benefits:

  • ~40% fewer tokens vs JSON
  • 73.9% accuracy vs 69.7% for JSON in retrieval tasks
  • Explicit length declarations for validation
  • Lossless JSON round-trips

Syntax

Objects (YAML-style indentation)

user:
  name: John
  age: 30
  address:
    city: NYC
    zip: 10001

Uniform Arrays (Tabular)

users[3]{id,name,email}:
  1,John,john@ex.com
  2,Jane,jane@ex.com
  3,Bob,bob@ex.com
  • [N] = array length (req for validation)
  • {fields} = column schema (declared once)

Scalar Arrays

tags[4]: api,rest,json,toon

Non-uniform Arrays (Nested)

items:
  - id: 1
    type: book
    meta:
      pages: 200
  - id: 2
    type: video
    meta:
      duration: 3600

Conversion Rules

JSON to TOON

  1. Objects => indented key-value pairs
  2. Uniform arrays => tabular [N]{fields}: format
  3. Mixed/nested arrays => - list notation
  4. Scalars => quote only when containing , or special chars

Examples

JSON:

{"orders":[{"id":1,"item":"Book","qty":2,"price":29.99},{"id":2,"item":"Pen","qty":10,"price":1.99}]}

TOON:

orders[2]{id,item,qty,price}:
  1,Book,2,29.99
  2,Pen,10,1.99

Nested JSON:

{"config":{"db":{"host":"localhost","port":5432},"cache":{"enabled":true,"ttl":300}}}

TOON:

config:
  db:
    host: localhost
    port: 5432
  cache:
    enabled: true
    ttl: 300

When to Use TOON

TOON replaces data serialization formats when sending to LLMs.

Formats to convert → TOON:

FormatConvert?Notes
JSONYesPrimary use case
JSON compactYesSame as JSON
YAMLYesStructured data
XMLYesVerbose, big savings

Do NOT convert to TOON:

FormatConvert?Reason
MarkdownNoKeep as markdown
Plain textNoKeep as text
Code filesNoKeep original syntax
CSVNoAlready compact for flat tables

TOON sweet spot:

Uniform arrays of objects (same fields per item) from JSON/YAML/XML.

Key insight:

TOON replaces data serialization formats when the consumer is an LLM.

Quick Reference

Data TypeTOON SyntaxExample
Objectindentuser:\n name: John
Uniform array[N]{fields}:items[2]{a,b}:\n 1,x\n 2,y
Scalar array[N]:ids[3]: 1,2,3
Nested array- item- name: x\n- name: y
Quoted str"val"name: "a,b,c"
Nullnullval: null
Booltrue/falseactive: true

File Format

  • Extension: .toon
  • Media type: text/toon
  • Encoding: UTF-8

Implementations

Official npm: @toon-format/toon, @toon-format/cli

Node.js/TypeScript Example

// npm install @toon-format/toon
import { encode, decode } from '@toon-format/toon';

// JSON to TOON
const data = { users: [{ id: 1, name: 'John' }] };
const toonStr = encode(data);

// TOON to JSON
const parsed = decode(toonStr);

CLI Example

# npm install -g @toon-format/cli
toon encode input.json > output.toon
toon decode input.toon > output.json

Validation

TOON's [N] notation enables:

  • Array truncation detection
  • Field count validation
  • Schema consistency checks
# This declares exactly 3 items w/ 2 fields each
products[3]{name,price}:
  Widget,9.99
  Gadget,19.99
  Tool,14.99

If LLM receives incomplete data, length mismatch signals corruption.

Scripts

Validator (scripts/validate.py)

Validates TOON syntax and structure before use.

# Validate TOON file
python .claude/skills/document-skills/toon/scripts/validate.py input.toon

# Check JSON compatibility for TOON conversion
python .claude/skills/document-skills/toon/scripts/validate.py --json input.json

# Quiet mode (errors only)
python .claude/skills/document-skills/toon/scripts/validate.py -q input.toon

Checks:

  • Array length [N] matches actual row count
  • Field count {a,b,c} matches values per row
  • Quote balance
  • Consistent indentation
  • Empty value warnings

Converter - Node.js (scripts/convert.js) - Recommended

Uses official @toon-format/toon library for full spec compliance.

# Install official library
npm install @toon-format/toon

# JSON to TOON
node .claude/skills/document-skills/toon/scripts/convert.js input.json

# JSON to TOON with output file
node .claude/skills/document-skills/toon/scripts/convert.js input.json -o output.toon

# TOON to JSON
node .claude/skills/document-skills/toon/scripts/convert.js --to-json input.toon

# Verify round-trip
node .claude/skills/document-skills/toon/scripts/convert.js --verify input.json

Converter - Python (scripts/convert.py) - Fallback

Basic implementation when Node.js is not available.

# JSON to TOON
python .claude/skills/document-skills/toon/scripts/convert.py input.json

# TOON to JSON
python .claude/skills/document-skills/toon/scripts/convert.py --to-json input.toon

# Verify round-trip
python .claude/skills/document-skills/toon/scripts/convert.py --verify input.json

Compression Tips

  1. Use tabular format for uniform arrays (max savings)
  2. Declare lengths [N] for validation
  3. Quote strings only when necessary
  4. Flatten when possible, nest when required
  5. Combine related arrays into single table

Source

git clone https://github.com/hackermanishackerman/claude-skills-vault/blob/main/.claude/skills/document-skills/toon/SKILL.mdView on GitHub

Overview

TOON (Token-Oriented Object Notation) is a compact encoding of JSON designed for LLM input. It blends YAML-style indentation with CSV-style tables to create human-readable data with ~40% fewer tokens and lossless round-trips, boosting retrieval accuracy versus JSON.

How This Skill Works

Objects become indented key-value pairs. Uniform arrays render as tabular sections using [N]{fields}:, and scalar arrays use [N]:. Non-uniform and nested arrays use dash notation. Explicit length declarations ([N]) enable validation and robust error detection during encoding and decoding.

When to Use It

  • When you want to minimize tokens sent to LLMs for structured data.
  • When working with uniform arrays of objects that share the same fields.
  • When you need explicit array length validation via [N] for reliable parsing.
  • When you value readable indentation plus compact tabular sections for LLM comprehension.
  • When building tooling with the TOON ecosystem (e.g., @toon-format/toon, CLI).

Quick Start

  1. Step 1: Install the Toon library (npm i @toon-format/toon) or use the CLI (@toon-format/cli).
  2. Step 2: Convert JSON to TOON with encode(data) and inspect the TOON string.
  3. Step 3: Convert back with decode(toonString) to obtain the original JSON.

Best Practices

  • Use uniform arrays for homogeneous items and declare length with [N]{fields}.
  • Quote scalars only if they contain commas or special characters to avoid unnecessary escaping.
  • Keep nested data readable with YAML-like indentation and avoid excessive depth.
  • Include explicit [N] lengths for validation and error detection during round-trips.
  • Prefer TOON for LLM-centric data interchange; keep JSON for interoperability where needed.

Example Use Cases

  • User list: users[2]{id,name,email}: 1,Alice,alice@example.com 2,Bob,bob@example.com
  • Config object: config: db: host: localhost port: 5432 cache: enabled: true ttl: 300
  • Product catalog: products[3]{id,name,price}: 1,Widget,9.99 2,Gadget,14.5 3,Thing,7.0
  • Tags: tags[4]: api,rest,json,toon
  • Nested example: orders: - id: 1 item: Book qty: 2 - id: 2 item: Pen qty: 10

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers