JSON to TOON
Convert between JSON and TOON (Token-Oriented Object Notation) for efficient LLM communication. TOON reduces token count by 30-60% while maintaining accuracy.
💡 About TOON Format
- • 30-60% fewer tokens on large uniform arrays vs JSON
- • Tabular arrays: Declare keys once, stream data as rows
- • Indentation-based: Like YAML, uses whitespace instead of braces
- • Minimal syntax: Removes redundant punctuation
1. What is TOON?
TOON (Token-Oriented Object Notation) is a data format designed to make communication with Large Language Models (LLMs) more accurate and token-efficient. It typically achieves 30-60% fewer tokens on large uniform arrays compared to formatted JSON. TOON uses a tabular format where keys are declared once and values are listed as rows, similar to CSV but with richer type support. This makes it ideal for API responses, database results, and agentic systems where token cost matters.
2. How does it work?
TOON optimizes JSON by using a tabular format for uniform arrays. Instead of repeating object keys for each array item, TOON declares the structure once using the format [length]{keys}: followed by comma-separated values. When converting JSON to TOON, uniform arrays are detected and transformed into this compact table format. When converting TOON back to JSON, the table structure is expanded into full JSON objects.
Use Cases
TOON is most effective for large arrays with uniform structure like API responses, database query results, and data tables. A typical shopping cart with 4 items can save 43% in tokens (~520 tokens in JSON vs ~295 in TOON).
When NOT to Use
Avoid TOON for deeply nested structures, non-uniform arrays, or small datasets where savings are minimal. Human readability should take priority over token efficiency in documentation and configuration files.
3. Examples
Simple array conversion
JSON: [{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]
↓
TOON: [2]{id,name}:
1,Alice
2,BobShopping cart (43% token savings)
JSON: {"items":[{"id":"A1","name":"iPhone","price":999},{"id":"A2","name":"MacBook","price":1999}]}
↓
TOON: items: [2]{id,name,price}:
A1,iPhone,999
A2,MacBook,19994. Source Code
1// TypeScript: Convert JSON to TOON and vice versa
2
3function jsonToToon(jsonString: string): string {
4 const obj = JSON.parse(jsonString);
5
6 // Check if uniform array
7 if (Array.isArray(obj) && obj.length > 0) {
8 const keys = Object.keys(obj[0]);
9 const isUniform = obj.every(item =>
10 Object.keys(item).length === keys.length &&