JSON to TOON
Convert JSON data to TOON format for compact data representation
Convert JSON data to TOON format for compact data representation
This document is a comprehensive technical guide covering both directions of conversion: JSON → TOON and TOON → JSON. It is written for engineers building LLM pipelines, validators, data converters, and structured-output systems.
TOON (Token-Oriented Object Notation) is a compact, deterministic, lossless representation of JSON. It was designed to reduce token usage inside LLM prompts and provide strict, self-validating structure that JSON does not offer.
JSON is familiar but verbose. In JSON:
TOON fixes these issues using:
Despite differences in form, TOON maintains full compatibility with the JSON data model.
TOON has two core encoding strategies:
Instead of braces, TOON uses indentation.
Example JSON:
{
"user": { "id": 5, "name": "Ada" }
}TOON:
user: id: 5 name: Ada
This reduces tokens and improves readability.
This is the main source of TOON's efficiency.
JSON:
[
{ "id": 1, "name": "Alice", "role": "admin" },
{ "id": 2, "name": "Bob", "role": "user" }
]TOON:
users[12]{id,name,role}:
1,Alice,admin
2,Bob,userThe [12] indicates the declared length, and {id,name,role} defines the schema.
JSON:
["ana", "luis", "sam"]
TOON:
friends[3]: ana,luis,sam
TOON avoids quotes unless needed, reducing tokens further.
A JSON to TOON converter reads JSON and transforms it into token-efficient TOON.
1. Parse JSON.
2. Detect uniform arrays.
3. Emit tabular arrays where possible.
4. Convert objects into indented form.
5. Apply TOON quoting rules.
6. Output deterministic TOON.
JSON:
{
"context": {
"task": "Our favorite hikes",
"location": "Boulder"
},
"friends": ["ana", "luis", "sam"],
"hikes": [
{ "id": 1, "name": "Blue Lake", "km": 7.5, "sunny": true },
{ "id": 2, "name": "Overlook", "km": 9.2, "sunny": false }
]
}TOON:
context:
task: Our favorite hikes
location: Boulder
friends[3]: ana,luis,sam
hikes[12]{id,name,km,sunny}:
1,Blue Lake,7.5,true
2,Overlook,9.2,falseBefore using TOON in LLM workflows, you should validate it. A TOON validator checks:
[N]This makes validate TOON format a critical part of pipelines.
TOON doesn't require separate schemas. The schema is declared inline:
items[4]{id,title,price}:A TOON schema validator simply confirms:
A TOON syntax checker ensures:
Converting TOON back into JSON is deterministic because TOON contains explicit metadata.
1. Parse indentation into objects.
2. Parse primitive arrays with declared length.
3. Parse tabular arrays using schema {fields}.
4. Convert rows into JSON objects.
5. Produce proper JSON.
TOON:
report:
id: 52
generatedAt: 2025-02-12
operators[3]: lee,jordan,mina
records[4]{id,status,latencyMs}:
1,ok,14
2,ok,16
3,fail,220
4,ok,13JSON:
{
"report": {
"id": 52,
"generatedAt": "2025-02-12"
},
"operators": ["lee", "jordan", "mina"],
"records": [
{ "id": 1, "status": "ok", "latencyMs": 14 },
{ "id": 2, "status": "ok", "latencyMs": 16 },
{ "id": 3, "status": "fail", "latencyMs": 220 },
{ "id": 4, "status": "ok", "latencyMs": 13 }
]
}TOON validators catch these errors before decoding.
1. JSON generated by backend
2. Convert JSON → TOON
3. Embed TOON in LLM prompt
4. LLM outputs TOON
5. Validate TOON
6. Convert TOON → JSON
7. Downstream tools consume JSON
LLMs often produce:
TOON validators detect these immediately.
export:
version: 3
source: monitor_alpha
generatedAt: 2025-02-12
operators[3]: lee,jordan,mina
sensors[5]{id,type,value,timestamp,isCritical}:
1,temp,21.4,2025-02-12T09:00Z,false
2,pressure,13.8,2025-02-12T09:00Z,true
3,temp,22.0,2025-02-12T09:10Z,false
4,humidity,43,2025-02-12T09:10Z,false
5,pressure,14.2,2025-02-12T09:10Z,trueThis converts cleanly to JSON due to deterministic structure.