Overview
TOON encoding converts JavaScript values (objects, arrays, primitives) into the token-efficient TOON format. The SDK provides multiple encoding functions for different use cases, from simple one-shot encoding to memory-efficient streaming for large datasets.Basic Encoding
Simple Objects
Theencode() function converts any JavaScript value to TOON format:
Nested Objects
Nested structures use indentation to show hierarchy:Uniform Arrays (Tabular Format)
Arrays of uniform objects are encoded as compact tables:Primitive Arrays
Arrays of primitives are encoded inline:Encoding Options
Customize encoding behavior with theEncodeOptions interface:
Custom Indentation
Tab Delimiter
Tabs are the most token-efficient delimiter:Tab delimiters (
\t) provide the best token efficiency, reducing tokens by ~5-10% compared to commas. Use tabs for LLM input where token count matters most.Pipe Delimiter
Pipes are useful when data contains commas:Key Folding
Key folding collapses single-key wrapper chains into dotted paths:Flatten Depth
Control how deep key folding can go:Value Transformation with Replacer
Thereplacer function provides fine-grained control over encoding, similar to JSON.stringify:
Removing Fields
Transforming Values
Path-Based Filtering
Use thepath parameter for context-aware transformations:
Adding Metadata
The replacer function receives
(key, value, path) where:keyis the property name or array index as a string (empty string''for root)valueis the normalized JSON valuepathis an array of keys/indices from root to current value
Streaming Large Datasets
For large datasets, useencodeLines() to avoid building the entire string in memory:
Streaming to stdout
Streaming with Options
Available Delimiters
The SDK provides three delimiter options:Delimiter Comparison
1
Choose your delimiter
Start with comma for readability. Switch to tab for LLM input to maximize token savings.
2
Configure options
Set indentation, key folding, and other options based on your data structure.
3
Add transformations
Use the replacer function to filter sensitive data or transform values.
4
Stream when needed
For large datasets, use
encodeLines() instead of encode() to avoid memory issues.API Reference
encode(input, options?)
Encodes a JavaScript value to TOON format string.
Parameters:
input: unknown- Any JavaScript valueoptions?: EncodeOptions- Optional configuration
string - TOON formatted string
Source: packages/toon/src/index.ts:48
encodeLines(input, options?)
Encodes a JavaScript value as an iterable of TOON lines.
Parameters:
input: unknown- Any JavaScript valueoptions?: EncodeOptions- Optional configuration
Iterable<string> - Iterable of TOON lines (without newlines)
Source: packages/toon/src/index.ts:99
Best Practices
Use tabs for LLM input
Tab delimiters reduce token count by ~5-10% compared to commas.
Stream large data
Use
encodeLines() for datasets over 10MB to avoid memory issues.Remove sensitive data
Use the replacer function to filter passwords, API keys, and PII.
Test round-trips
Verify that
decode(encode(data)) equals original data.Next Steps
Decoding TOON
Learn how to parse TOON format back into JavaScript values
Streaming
Process large datasets with streaming encode/decode APIs
LLM Integration
Use TOON format effectively with Large Language Models
