AgentSkillsCN

toon

将JSON转换为紧凑的TOON格式,以降低Token消耗。当您需要检查JSON API、处理大型JSON负载,或在向LLM发送数据时降低Token成本时,可使用此技能。

SKILL.md
--- frontmatter
name: toon
description: Convert JSON to compact TOON format for reduced token usage. Use when inspecting JSON APIs, processing large JSON payloads, or reducing token costs when sending data to LLMs.

TOON CLI

TOON (Token-Optimized Object Notation) reduces JSON token counts by 30-50%. Convert JSON ↔ TOON for API inspection, large payload processing, or LLM context optimization.

Quick Reference

Convert JSON to TOON

bash
# From file
toon data.json -o output.toon

# From stdin (pipe from curl, jq, etc.)
curl -s https://api.example.com/data | npx @toon-format/cli

# With token statistics
toon data.json --stats

# Pipe from jq
jq '.results' data.json | npx @toon-format/cli --stats

Convert TOON to JSON

bash
# From file
toon data.toon -o output.json

# From stdin
cat data.toon | toon --decode

Common Options

OptionDescription
-o, --output <file>Output file (stdout if omitted)
-e, --encodeForce encode mode (JSON → TOON)
-d, --decodeForce decode mode (TOON → JSON)
--statsShow token savings (encode only)
--delimiter <char>Array delimiter: , (default), \t, `
--keyFolding safeCollapse nested objects to dotted paths
--expandPaths safeExpand dotted paths when decoding

Use Cases

Inspect API Response

bash
# Quick preview of API data in compact form
curl -s https://api.github.com/users/torvalds | npx @toon-format/cli

# With token count comparison
curl -s https://api.github.com/repos/torvalds/linux | npx @toon-format/cli --stats

Process Large JSON Files

bash
# Convert large file with streaming (memory efficient)
toon huge-dataset.json -o output.toon

# Check token savings before processing
toon large-api-response.json --stats

Optimize for LLM Context

bash
# Maximum compression: key folding + tab delimiter
toon data.json --keyFolding safe --delimiter "\t" --stats -o compressed.toon

# Round-trip: encode with folding, decode with expansion
toon input.json --keyFolding safe -o compressed.toon
toon compressed.toon --expandPaths safe -o restored.json

Pipeline Integration

bash
# Filter with jq, compress with toon
jq '.data.items[:10]' response.json | npx @toon-format/cli > subset.toon

# Fetch, transform, compress
curl -s https://api.example.com/data | jq '.results' | npx @toon-format/cli --stats

Format Overview

TOON uses indentation instead of braces and brackets, with inline arrays and tabular object arrays:

JSON:

json
{
  "user": {
    "id": 123,
    "name": "Ada",
    "tags": ["admin", "ops"]
  },
  "items": [
    {"sku": "A1", "qty": 2, "price": 9.99},
    {"sku": "B2", "qty": 1, "price": 14.5}
  ]
}

TOON:

yaml
user:
  id: 123
  name: Ada
  tags[2]: admin,ops
items[2]{sku,qty,price}:
  A1,2,9.99
  B2,1,14.5

Key Features

  • No braces/brackets: Indentation defines structure
  • Inline primitive arrays: tags[2]: admin,ops instead of ["admin","ops"]
  • Tabular objects: Arrays of uniform objects become CSV-like tables
  • Optional key folding: data.metadata.items[2]: a,b instead of nested objects
  • Unquoted strings: Only quote when necessary (special chars, delimiters)

Token Savings Example

bash
$ curl -s https://api.github.com/users/torvalds | npx @toon-format/cli --stats

✔ Encoded stdin → stdout

ℹ Token estimates: ~245 (JSON) → ~156 (TOON)
✔ Saved ~89 tokens (-36.3%)

Related