Skip to main content

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

The encode() 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 the EncodeOptions 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:
Key folding only works with expandPaths: 'safe' during decoding to ensure lossless round-trips. See Decoding Guide.

Flatten Depth

Control how deep key folding can go:

Value Transformation with Replacer

The replacer function provides fine-grained control over encoding, similar to JSON.stringify:

Removing Fields

Transforming Values

Path-Based Filtering

Use the path parameter for context-aware transformations:

Adding Metadata

The replacer function receives (key, value, path) where:
  • key is the property name or array index as a string (empty string '' for root)
  • value is the normalized JSON value
  • path is an array of keys/indices from root to current value

Streaming Large Datasets

For large datasets, use encodeLines() to avoid building the entire string in memory:

Streaming to stdout

Streaming with Options

encodeLines() is an iterable that yields lines one at a time. It’s perfect for:
  • Writing to files without memory overhead
  • Streaming HTTP responses
  • Processing large datasets chunk by chunk

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 value
  • options?: EncodeOptions - Optional configuration
Returns: 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 value
  • options?: EncodeOptions - Optional configuration
Returns: 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