docs/reference/syntax-cheatsheet.md
Quick reference for mapping JSON to TOON format. For rigorous, normative syntax rules and edge cases, see the Specification.
::: code-group
{
"id": 1,
"name": "Ada"
}
id: 1
name: Ada
:::
::: code-group
{
"user": {
"id": 1,
"name": "Ada"
}
}
user:
id: 1
name: Ada
:::
::: code-group
{
"tags": ["foo", "bar", "baz"]
}
tags[3]: foo,bar,baz
:::
::: code-group
{
"items": [
{ "id": 1, "qty": 5 },
{ "id": 2, "qty": 3 }
]
}
items[2]{id,qty}:
1,5
2,3
:::
::: code-group
{
"items": [1, { "a": 1 }, "x"]
}
items[3]:
- 1
- a: 1
- x
:::
[!NOTE] When a list-item object has a tabular array as its first field, the tabular header appears on the hyphen line. Rows are indented two levels deeper than the hyphen, and other fields are indented one level deeper. This is the canonical encoding for this pattern.
::: code-group
items[1]:
- users[2]{id,name}:
1,Ada
2,Bob
status: active
items[1]:
- users[2]{id,name}:
1,Ada
2,Bob
:::
::: code-group
{
"pairs": [[1, 2], [3, 4]]
}
pairs[2]:
- [2]: 1,2
- [2]: 3,4
:::
::: code-group
["x", "y", "z"]
[3]: x,y,z
:::
::: code-group
{}
(empty output)
:::
::: code-group
{
"items": []
}
items: []
:::
::: code-group
{
"version": "123",
"enabled": "true"
}
version: "123"
enabled: "true"
:::
These strings must be quoted because they look like numbers/booleans.
::: code-group
{
"note": "hello, world"
}
note: "hello, world"
:::
Strings must be quoted when they contain the active delimiter (inside an array scope) or the document delimiter (object field values, comma by default).
::: code-group
{
"message": " padded "
}
message: " padded "
:::
::: code-group
{
"name": ""
}
name: ""
:::
Strings must be quoted if they:
"")true, false, or null (case-sensitive)"42", "-3.14", "1e-6", "05"):, ", \, [, ], {, }, or any control character (U+0000–U+001F, including newline/tab/CR)"-" or start with "-" followed by any characterOtherwise, strings can be unquoted. Unicode and emoji are safe:
message: Hello 世界 👋
note: This has inner spaces
Six escape sequences are valid in quoted strings:
| Character | Escape |
|---|---|
Backslash (\) | \\ |
Double quote (") | \" |
| Newline | \n |
| Carriage return | \r |
| Tab | \t |
| Any other U+0000–U+001F control character | \uXXXX |
Other escapes (e.g., \x, \0, \b) are invalid, and lone-surrogate \uXXXX values (U+D800–U+DFFF) are rejected.
key[N]:
N = array lengthkey[N]{field1,field2,field3}:
N = array length{fields} = column names::: code-group
items[2 ]{id name}:
1 Alice
2 Bob
items[2|]{id|name}:
1|Alice
2|Bob
:::
The delimiter symbol appears inside the brackets and braces.
Standard nesting:
data:
metadata:
items[2]: a,b
With key folding (keyFolding: 'safe'):
data.metadata.items[2]: a,b
See Format Overview – Key Folding for details.
| Input | Output |
|---|---|
| Finite number | Canonical decimal (no exponent, no trailing zeros) |
NaN, Infinity, -Infinity | null |
BigInt (safe range) | Number |
BigInt (out of range) | Quoted decimal string |
Date | ISO string (quoted) |
Set | Array of normalized values |
Map | Object with String(key) keys |
undefined, function, symbol | null |
::: info
TOON itself doesn't specify how Date should be encoded – the spec leaves this to implementations. This library emits an ISO 8601 string in quotes; other implementations may choose differently.
:::