How to Convert JSON to TOON: 4 Methods Compared
JSON works fine for APIs, but it’s inefficient for LLM inputs. Every repeated key, every bracket, every quote consumes tokens. TOON (Token-Oriented Object Notation) reduces token usage by 30-60% while preserving the exact same data structure.
This guide covers four ways to convert JSON to TOON, from code libraries to desktop apps, so you can pick the right tool for your workflow.
What Is TOON?
TOON is a data serialization format designed specifically for LLM inputs. It uses YAML-like indentation for nested structures and CSV-style rows for uniform arrays, eliminating redundant syntax while remaining human-readable.
JSON (51 tokens):
{
"users": [
{ "id": 1, "name": "Alice", "role": "admin" },
{ "id": 2, "name": "Bob", "role": "user" }
]
}
TOON (24 tokens):
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
Same data. Half the tokens. The schema is declared once in the header, then each row contains only values—no repeated keys, no brackets, no quotes around simple strings.
Method 1: Code Libraries
Use this when you’re building software that sends data to LLMs.
TOON has official implementations for JavaScript, Python, Java, Julia, and Rust, plus community libraries for 15+ other languages including Go, PHP, Ruby, Swift, Kotlin, C++, Elixir, and R. Check the official TOON repository for the full list.
Here are examples for the two most common languages:
JavaScript/TypeScript
Install the official package:
npm install @toon-format/toon
Convert JSON to TOON:
import { encode, decode } from '@toon-format/toon';
const data = {
users: [
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' }
]
};
const toon = encode(data);
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
const restored = decode(toon); // Back to JSON
Python
pip install toon-llm
from toon_llm import ToonSerializer
data = {
"users": [
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"}
]
}
toon_string = ToonSerializer.serialize(data)
When to use: You’re building an application that sends data to LLMs and want to convert programmatically before each API call.
Method 2: Command Line
Best for quick one-off conversions, shell scripts, or piping data between tools.
The official CLI auto-detects format from file extensions:
# JSON to TOON
npx @toon-format/cli data.json -o data.toon
# TOON to JSON
npx @toon-format/cli data.toon -o output.json
# Pipe from stdin
cat api-response.json | npx @toon-format/cli > optimized.toon
# Show token savings
npx @toon-format/cli data.json --stats
When to use: You have JSON files on disk and want to convert them without writing code. Good for scripting and automation.
Method 3: Online Converters
Best for quick tests with non-sensitive data.
Several browser-based tools exist:
- toonformat.dev/playground — Official TOON playground with token comparison
- toontools.vercel.app — Simple converter interface
All process data in-browser (client-side JavaScript), so data technically doesn’t hit their servers. But you’re still loading external scripts, and there’s no way to verify what happens with your input.
When to use: Testing TOON syntax with sample data. Avoid for anything containing API keys, user data, or proprietary information.
Method 4: Desktop Apps (Offline)
Best for sensitive data, air-gapped environments, or developers who want a permanent tool.
PrimeUtils is a macOS toolkit with 100+ developer utilities, including a TOON converter that works entirely offline. No network calls, no external dependencies—your data never leaves your machine.
What sets it apart:
- True offline operation — Works without internet. Verify with Little Snitch or a packet sniffer if you’re skeptical.
- Multi-format support — Convert JSON, YAML, XML, or CSV to TOON directly. No need to convert to JSON first.
- Bidirectional — TOON back to any supported format with one click.
- Part of a larger toolkit — JWT debugger, Base64 encoder, hash generators, regex tester, and 100+ other utilities in one app.
The workflow is straightforward: select your input format, paste or load your data, click Convert. Output appears instantly with a copy button.
When to use: You’re working with production data, client information, or anything you can’t paste into a web form. Also useful if you regularly switch between JSON, YAML, and other formats.
PrimeUtils is a paid app (one-time purchase), with a demo version available to test core features.
When TOON Doesn’t Help
TOON isn’t universally better than JSON. Skip it when:
- Data is deeply nested with non-uniform structures — TOON’s efficiency comes from tabular arrays. Complex nested configs may actually use more tokens.
- Arrays have inconsistent schemas — If objects in an array have different fields, TOON can’t use its compact row format.
- You need pure tabular data — For flat tables with no nesting, CSV is 5-10% smaller than TOON.
- Latency matters more than tokens — Serialization adds overhead. For real-time applications, benchmark both formats.
TOON’s sweet spot: uniform arrays of objects with consistent fields across hundreds or thousands of rows.
Quick Comparison
| Method | Best For | Offline | Multi-Format |
|---|---|---|---|
| Code Libraries | Automated pipelines | Yes | No (JSON only) |
| CLI | Scripts, one-off files | Yes | No (JSON only) |
| Online Tools | Quick tests | No | No |
| PrimeUtils | Sensitive data, daily use | Yes | Yes (JSON, YAML, XML, CSV) |
Conclusion
Converting JSON to TOON is straightforward regardless of which method you choose. The right tool depends on your workflow:
- Building an app? Use the code libraries.
- Scripting? Use the CLI.
- Quick test? Online converters work.
- Sensitive data or multi-format needs? A desktop tool like PrimeUtils keeps everything local.
Start with whatever matches your current task. The 30-60% token savings compound quickly when you’re making thousands of LLM API calls.