IDENTIFICATION DIVISION.
PROGRAM-ID. PRODUCT-LIST.
DATA DIVISION.
WORKING-STORAGE SECTION.
*> 1. Define the raw data for the table.
*> Each record is 18 bytes:
*> ID (2) = 01
*> Name (10) = "Laptop "
*> Price (6) = 399990 (for 9(4)V99)
01 WS-PRODUCT-TABLE-DATA.
05 FILLER PIC X(18) VALUE "01Laptop 399990".
05 FILLER PIC X(18) VALUE "02Mouse 014990".
05 FILLER PIC X(18) VALUE "03Headset 049900".
*> 2. Redefine that block of memory as a structured COBOL table.
*> This maps the fields to the raw data above.
01 WS-PRODUCT-TABLE REDEFINES WS-PRODUCT-TABLE-DATA.
05 WS-PRODUCT-ENTRY OCCURS 3 TIMES.
10 WS-PRODUCT-ID PIC 9(02).
10 WS-PRODUCT-NAME PIC X(10).
10 WS-PRODUCT-PRICE PIC 9(04)V99. *> Implied decimal
*> 3. Define helper variables for looping and display
01 WS-INDEX PIC 9(01).
01 WS-DISPLAY-PRICE PIC Z,ZZ9.99. *> For formatting the output
PROCEDURE DIVISION.
MAIN-PROCEDURE.
*> Loop through the table (from 1 to 3) and display each record
PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > 3
DISPLAY "--------------------------"
DISPLAY "Product Record: " WS-INDEX
*> Access data using the index: WS-FIELD-NAME(WS-INDEX)
DISPLAY " ID: " WS-PRODUCT-ID(WS-INDEX)
DISPLAY " Name: " WS-PRODUCT-NAME(WS-INDEX)
*> Move the computational price (9V9) to a display-ready
*> field (Z,ZZ9.99) to add the decimal point and $
MOVE WS-PRODUCT-PRICE(WS-INDEX) TO WS-DISPLAY-PRICE
DISPLAY " Price: " WS-DISPLAY-PRICE
END-PERFORM.
DISPLAY "--------------------------".
STOP RUN.
Our system processes billions of tokens each month from the database alone, we can't use YAML or JSON because they are too token heavy. For flat results (like a database query) we simply return it as CSV (which is even 30% fewer tokens than TOON). We haven't adopted TOON yet (we have our own for structured objects) but its definitely more token friendly than YAML. At the scale we operate, YAML is expensive.
I based my statement on the examples I have seen at that moment. And I agree if you use YAML for tabular data it is expensive. That is why in my other comment I mentioned a switch based on the shape of the data. CSV for tabular data and YAML for hierarchical data.
You can even have CSV in YAML.
people:|id,name,age1,Alice,302,Bob,25
If that wasn't possible, I would go all in for TOON for those mixed cases.
TOON is YAML with hierarchical data. So it doesn't reduces tokens. And as you mention, in the case of tabular data CSV is better.
If you can show where TOON is saving tokens over the smart use of YAML and CSV, I'm glad to stand corrected.
True but why the need to invent a new format?
Most languages have mature YAML and CSV libraries if you need to condense the text that is send to an AI.
The main reason for TOON is probably that you can feed it content that is better compacted by YAML and better compacted by CSV. Instead of creating a function to switch the output between the two formats yourself.
Out of curiosity I asked an AI to create that function.
/**
* Detects whether a JSON object represents a tree‑like structure
* or a tabular (array‑of‑objects) structure, then converts it to
* YAML or CSV accordingly.
*
* @param {object|array} data - Parsed JSON data.
* @returns {string} YAML string for tree‑like data or CSV string for tabular data.
*/functionconvertJson(data){// Helper: check if value is a plain object (not array, not null)constisObject=v=>v&&typeofv==='object'&&!Array.isArray(v);// Detect tabular: an array where every element is an object// and each object has the same set of primitive keys.constisTabular=arr=>{if (!Array.isArray(arr)||arr.length===0)returnfalse;// All elements must be objects (no nested arrays/objects as values)constfirstKeys=Object.keys(arr[0]);if (firstKeys.length===0)returnfalse;returnarr.every(item=>{if (!isObject(item))returnfalse;constkeys=Object.keys(item);// same keys as first rowif (keys.length!==firstKeys.length)returnfalse;for (letkoffirstKeys){if (!keys.includes(k))returnfalse;// values should be primitive (string, number, boolean, null)constv=item[k];if (v&&typeofv==='object')returnfalse;}returntrue;});};// Convert tabular data to CSVconsttoCsv=arr=>{constheaders=Object.keys(arr[0]);constescape=v=>{if (v==null)return'';consts=String(v);returns.includes(',')||s.includes('"')||s.includes('\n')?`"${s.replace(/"/g,'""')}"`:s;};constrows=arr.map(row=>headers.map(h=>escape(row[h])).join(','));return[headers.join(','),...rows].join('\n');};// Convert any object/array to YAML (simple implementation)consttoYaml=obj=>{constyaml=require('js-yaml');// assumes js-yaml is availablereturnyaml.dump(obj,{noRefs:true,indent:2});};// Main logicif (Array.isArray(data)&&isTabular(data)){returntoCsv(data);}else{// For tree‑like structures we fall back to YAML// If js-yaml is not available, a minimal serializer could be used.try{returntoYaml(data);}catch (e){// Minimal fallback YAML serializerconstserialize=(value,indent=0)=>{constpad=''.repeat(indent);if (Array.isArray(value)){returnvalue.map(v=>`${pad}- ${serialize(v,indent+2).trimStart()}`).join('\n');}elseif (isObject(value)){returnObject.entries(value).map(([k,v])=>`${pad}${k}: ${serialize(v,indent+2).trimStart()}`).join('\n');}else{return`${value}`;}};returnserialize(data);}}}
I’ve been experimenting with this new “code execution with MCP” concept and recently implemented it in my OSS mcpproxy project. The current version can generate a JSON → TOON converter on the fly for any MCP tool within an agent session, which makes data format conversion and reuse incredibly easy.
Prompt example: Read this post: dev.to/akki907/toon-vs-json-the-new-format-designed-for-ai-nk5. Then implement and run code that converts the tool's JSON output to TOON format, using the tool. Output ONLY TOON data in code execution response
Surely XML would do better in peak accuracy because a close tag explicitly matches to an opening tag?
Surely Markdown and YAML offer similar compactness and readability to TOON with better support?
It would be good to see the article amended to test against the JSON, XML, YAML, Markdown and CSV (or perhaps better still another delimited format like tab-delimited) data, to get a better idea as to how it compares to a more complete range of options
I am a Full-stack developer learning and building scalable web apps with MERN + TypeScript. Sharing insights from my journey in web development, DSA, and AI integration.
Thanks for sharing this. Built a quick tool for anyone wanting to test TOON formatting: bestaitools.tech/tools/json-to-toon Runs client-side, no data sent to servers 🛠️
Full-stack developer with 10+ years building software and managing infrastructure. Now focused on penetration testing, vulnerability research, and responsible disclosure.
Location
Latvia
Work
Full-stack dev & development manager. Co-owner @ hive5.
Thank you for the article, would you mind adding our JSON to TOON tool in your article?
scalevise.com/json-toon-converter
Cobol is that you?
Why not use YAML if you want a more condensed format?
Our system processes billions of tokens each month from the database alone, we can't use YAML or JSON because they are too token heavy. For flat results (like a database query) we simply return it as CSV (which is even 30% fewer tokens than TOON). We haven't adopted TOON yet (we have our own for structured objects) but its definitely more token friendly than YAML. At the scale we operate, YAML is expensive.
I based my statement on the examples I have seen at that moment. And I agree if you use YAML for tabular data it is expensive. That is why in my other comment I mentioned a switch based on the shape of the data. CSV for tabular data and YAML for hierarchical data.
You can even have CSV in YAML.
If that wasn't possible, I would go all in for TOON for those mixed cases.
TOON is YAML with hierarchical data. So it doesn't reduces tokens. And as you mention, in the case of tabular data CSV is better.
If you can show where TOON is saving tokens over the smart use of YAML and CSV, I'm glad to stand corrected.
I got that TOON is one more compact data serialization format, similar in purpose to YAML
True but why the need to invent a new format?
Most languages have mature YAML and CSV libraries if you need to condense the text that is send to an AI.
The main reason for TOON is probably that you can feed it content that is better compacted by YAML and better compacted by CSV. Instead of creating a function to switch the output between the two formats yourself.
Out of curiosity I asked an AI to create that function.
I’ve been experimenting with this new “code execution with MCP” concept and recently implemented it in my OSS mcpproxy project. The current version can generate a JSON → TOON converter on the fly for any MCP tool within an agent session, which makes data format conversion and reuse incredibly easy.
Prompt example: Read this post: dev.to/akki907/toon-vs-json-the-new-format-designed-for-ai-nk5. Then implement and run code that converts the tool's JSON output to TOON format, using the tool. Output ONLY TOON data in code execution response
Surely XML would do better in peak accuracy because a close tag explicitly matches to an opening tag?
Surely Markdown and YAML offer similar compactness and readability to TOON with better support?
It would be good to see the article amended to test against the JSON, XML, YAML, Markdown and CSV (or perhaps better still another delimited format like tab-delimited) data, to get a better idea as to how it compares to a more complete range of options
Thanks for the article. Had read many articles, but this one explains in much better way!
Thanks for sharing this. Built a quick tool for anyone wanting to test TOON formatting: bestaitools.tech/tools/json-to-toon Runs client-side, no data sent to servers 🛠️
Thanks for sharing.
Thank you for the article)
Why would you pick TOON over CSV?
I'll just leave this here for people still taking this seriously 👀
How it is different from TONL github.com/tonl-dev/tonl ?
Thorough resource on how to use TOON, thanks! I've made a free converter from JSON to TOON to showcase the library functionality.
Using this format for somedays and can see the token reductions. Anyone who wants to try could use this website for a quick POC: toonformatter.net