Skip to main content

Format Overview

TOON syntax reference with concrete examples. This page covers the complete format specification.

Data Model

TOON models data the same way as JSON:
  • Primitives: strings, numbers, booleans, and null
  • Objects: mappings from string keys to values
  • Arrays: ordered sequences of values
TOON is fully lossless: decode(encode(x)) always equals x after normalization of non-JSON types like Date, NaN, etc.

Root Forms

A TOON document can represent three root forms:
Fields appear at depth 0 with no parent key:
This is the most common form for structured data.
Begins with [N]: or [N]{fields}: at depth 0:
Or for primitive arrays:
A single primitive value:
Or:

Objects

Simple Objects

Objects with primitive values use key: value syntax, one field per line:
Syntax rules:
  • One space follows the colon
  • Indentation replaces braces
  • Keys are unquoted when safe (see Quoting Rules)

Nested Objects

Nested objects add one indentation level (default: 2 spaces):
When a key ends with : and has no value on the same line, it opens a nested object. All lines at the next indentation level belong to that object.

Empty Objects

An empty object at the root yields an empty document (no lines). A nested empty object is key: alone, with no children:
This represents { "data": {}, "status": "ready" }.

Arrays

TOON detects array structure and chooses the most efficient representation. Arrays always declare their length in brackets: [N].

Primitive Arrays (Inline)

Arrays of primitives render inline:
Strings containing the active delimiter (comma by default) must be quoted:

Tabular Arrays (Uniform Objects)

When all objects in an array share the same set of primitive-valued keys, TOON uses tabular format:
Header breakdown:
  • items — Array key name
  • [2] — Array length (2 rows)
  • {sku,qty,price} — Field names
  • : — Opens the array body
Each row:
  • Values in the same order as field list
  • Separated by the active delimiter (comma by default)
  • Encoded as primitives (strings, numbers, booleans, null)
An array qualifies for tabular format when:
  1. All elements are objects
  2. All objects have identical field sets (same keys, any order)
  3. All values are primitives (no nested arrays/objects)
If any condition fails, TOON uses list format instead.

Complex Tabular Example

Here’s a realistic example with various data types:
This includes:
  • Numbers: id, salary
  • Strings: name, department, startDate
  • Booleans: active

Non-Uniform Arrays (List Format)

Arrays that don’t meet tabular requirements use list format with hyphen markers:
Each element starts with - at one indentation level deeper than the parent array header.

Objects as List Items

When array elements are objects:
Notice the second object has an additional extra field—this non-uniformity requires list format.

Arrays of Arrays

When you have arrays containing primitive inner arrays:
Each inner array gets its own header on the list-item line. Nested uniform objects:

Empty Arrays

Empty arrays declare length zero with no elements:
No indented lines follow the header.

Array Headers

Header Syntax

Array headers follow this pattern:
Where:
  • N — Non-negative integer length
  • delimiter (optional) — Explicitly declares the active delimiter
  • fields (optional) — For tabular arrays: {field1,field2,field3}
The array length [N] helps LLMs validate structure. If you ask a model to generate TOON output, explicit lengths let you detect truncation or malformed data.

Delimiter Options

TOON supports three delimiters:
The comma is the default delimiter and requires no special declaration:
Strings containing commas must be quoted:
Declare tab with \t inside brackets:
Benefits:
  • Often tokenizes more efficiently than commas
  • Strings with commas don’t need quoting
  • Better for data with many punctuation marks
Usage:
Declare pipe with | inside brackets:
Benefits:
  • Visual separation without quoting commas or tabs
  • Useful for data that naturally contains both commas and tabs
Usage:
Delimiter scoping: The delimiter is scoped to the array header that declares it. Nested arrays can use different delimiters.

Quoting Rules

When Strings Need Quotes

TOON quotes strings only when necessary to maximize token efficiency. A string must be quoted if it:
  1. Is empty ("")
  2. Has leading or trailing whitespace
  3. Equals true, false, or null (case-sensitive)
  4. Looks like a number (e.g., "42", "-3.14", "1e-6", "05")
  5. Contains special characters: :, ", \, [, ], {, }, newline, tab, carriage return
  6. Contains the active delimiter
  7. Equals "-" or starts with "-" followed by any character

Escape Sequences

In quoted strings and keys, only five escape sequences are valid:
All other escape sequences (e.g., \x, \u, \0) are invalid and will cause an error in strict mode.

Type Conversions

TOON normalizes non-JSON types before encoding:

Numbers

Numbers are emitted in canonical decimal form:
Decoders accept both decimal and exponent forms on input (42, -3.14, 1e-6).

Special Values

BigInt

Date Objects

Date objects serialize to ISO 8601 strings:

Custom Serialization with toJSON

Objects with a toJSON() method are serialized by calling the method:

Key Folding (Optional)

Key folding is an optional encoder feature that collapses chains of single-key objects into dotted paths.

Basic Folding

Standard nesting:
With key folding (keyFolding: 'safe'):
The three nested objects collapse into a single dotted key data.metadata.items.

When Folding Applies

A chain of objects is foldable when:
  • Each object in the chain has exactly one key
  • The leaf value is a primitive, array, or empty object
  • All segments are valid identifiers: ^[A-Za-z_][A-Za-z0-9_]*$
  • No segment requires quoting
  • The resulting key doesn’t collide with existing keys
Foldable:
Not foldable (branch):
Not foldable (invalid segment):

Round-Trip with Path Expansion

When decoding TOON that used key folding, enable path expansion to restore the nested structure:
Path expansion is off by default, so dotted keys are treated as literal keys unless explicitly enabled.

Advanced Features

Tabular Header on List Items

When a list-item object has a tabular array as its first field, the tabular header appears on the hyphen line:
Indentation:
  • Tabular rows: Two levels deeper than hyphen
  • Other fields: One level deeper than hyphen

Custom Indentation

The default indentation is 2 spaces, but you can customize it:

Streaming Large Datasets

For memory-efficient processing of large datasets:
For streaming decode APIs, see decodeFromLines() and decodeStream().

Next Steps

When to Use TOON

Learn when TOON excels and when to use alternatives

Quick Start

Install the library and start encoding your data

API Reference

Explore encoding and decoding options

Advanced Options

Configuration options and fine-tuning