Skip to main content

Overview

TOON decoding converts TOON format strings back into JavaScript values (objects, arrays, primitives). The SDK provides multiple decoding functions with options for validation, path expansion, and streaming processing.

Basic Decoding

Simple Objects

The decode() function parses TOON format into JavaScript:

Nested Objects

Nested structures are reconstructed from indentation:

Tabular Arrays

TOON’s tabular format is decoded back into arrays of objects:

Primitive Arrays

Inline primitive arrays are parsed correctly:

Decoding Options

Customize decoding behavior with the DecodeOptions interface:

Custom Indentation

Match the indentation used during encoding:

Strict Mode

Strict mode validates array lengths and field counts:
Strict mode is enabled by default (strict: true). It catches truncated data, missing rows, and field count mismatches. Disable for lenient parsing: { strict: false }.

Path Expansion

Path expansion reconstructs dotted keys into nested objects:
Only use expandPaths: 'safe' when decoding data that was encoded with keyFolding: 'safe'. This ensures lossless round-trips.

Validation and Error Handling

Array Length Validation

TOON’s [N] syntax enables automatic validation:

Field Count Validation

Strict mode validates field counts match headers:

Lenient Mode

Disable strict validation for flexible parsing:
Use strict mode for production data validation. Use lenient mode when parsing potentially truncated or incomplete data from external sources.

Decoding from Lines

When you already have lines as an array, use decodeFromLines():

With Options

Handling Different Delimiters

The decoder automatically detects delimiters from the TOON format:

Comma Delimiter

Tab Delimiter

Pipe Delimiter

Type Safety

Decoded values follow the JSON type system:

Round-Trip Encoding

Verify lossless conversion:

With Key Folding

1

Parse TOON string

Use decode() for strings or decodeFromLines() for pre-split lines.
2

Configure validation

Enable strict mode (strict: true) to catch malformed data.
3

Handle path expansion

Use expandPaths: 'safe' when data was encoded with keyFolding: 'safe'.
4

Handle errors

Wrap decode calls in try-catch to handle validation errors gracefully.

API Reference

decode(input, options?)

Decodes a TOON format string into a JavaScript value. Parameters:
  • input: string - TOON formatted string
  • options?: DecodeOptions - Optional configuration
Returns: JsonValue - Parsed JavaScript value Source: packages/toon/src/index.ts:70

decodeFromLines(lines, options?)

Decodes TOON format from pre-split lines. Parameters:
  • lines: Iterable<string> - Iterable of TOON lines (without newlines)
  • options?: DecodeOptions - Optional configuration
Returns: JsonValue - Parsed JavaScript value Source: packages/toon/src/index.ts:129

Best Practices

Enable strict mode

Use strict: true in production to catch malformed data early.

Match indentation

Ensure decoder indent option matches the encoded data.

Test round-trips

Verify decode(encode(data)) equals original data.

Handle errors

Wrap decode calls in try-catch for graceful error handling.

Common Issues

Indentation Mismatch

Missing Path Expansion

Next Steps

Streaming Decode

Process large TOON files with streaming APIs

Encoding

Learn how to encode JavaScript values to TOON format

LLM Integration

Use TOON format effectively with Large Language Models