DEV Community

Cover image for TOON vs JSON: The New Format Designed for AI

TOON vs JSON: The New Format Designed for AI

Akash Thakur on November 09, 2025

How a novel data format is saving developers 30-60% on LLM token costs If you've been working with Large Language Models, you've probably noticed ...
Collapse
 
alifar profile image
Ali Farhat • Edited

Thank you for the article, would you mind adding our JSON to TOON tool in your article?
scalevise.com/json-toon-converter

Collapse
 
ti_li_4d842ff8c7d8d71 profile image
铁泪Tiě Lèi

Cobol is that you?

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.
Enter fullscreen mode Exit fullscreen mode
Collapse
 
xwero profile image
david duymelinck

Why not use YAML if you want a more condensed format?

Collapse
 
ievolved profile image
Shawn Bullock

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.

Collapse
 
xwero profile image
david duymelinck

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,age
  1,Alice,30
  2,Bob,25
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
marcu_loreto_3cabce9877ee profile image
Marcu Loreto

I got that TOON is one more compact data serialization format, similar in purpose to YAML

Collapse
 
xwero profile image
david duymelinck

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.
 */
function convertJson(data) {
  // Helper: check if value is a plain object (not array, not null)
  const isObject = v => v && typeof v === 'object' && !Array.isArray(v);

  // Detect tabular: an array where every element is an object
  // and each object has the same set of primitive keys.
  const isTabular = arr => {
    if (!Array.isArray(arr) || arr.length === 0) return false;
    // All elements must be objects (no nested arrays/objects as values)
    const firstKeys = Object.keys(arr[0]);
    if (firstKeys.length === 0) return false;

    return arr.every(item => {
      if (!isObject(item)) return false;
      const keys = Object.keys(item);
      // same keys as first row
      if (keys.length !== firstKeys.length) return false;
      for (let k of firstKeys) {
        if (!keys.includes(k)) return false;
        // values should be primitive (string, number, boolean, null)
        const v = item[k];
        if (v && typeof v === 'object') return false;
      }
      return true;
    });
  };

  // Convert tabular data to CSV
  const toCsv = arr => {
    const headers = Object.keys(arr[0]);
    const escape = v => {
      if (v == null) return '';
      const s = String(v);
      return s.includes(',') || s.includes('"') || s.includes('\n')
        ? `"${s.replace(/"/g, '""')}"`
        : s;
    };
    const rows = arr.map(row => headers.map(h => escape(row[h])).join(','));
    return [headers.join(','), ...rows].join('\n');
  };

  // Convert any object/array to YAML (simple implementation)
  const toYaml = obj => {
    const yaml = require('js-yaml'); // assumes js-yaml is available
    return yaml.dump(obj, { noRefs: true, indent: 2 });
  };

  // Main logic
  if (Array.isArray(data) && isTabular(data)) {
    return toCsv(data);
  } else {
    // For tree‑like structures we fall back to YAML
    // If js-yaml is not available, a minimal serializer could be used.
    try {
      return toYaml(data);
    } catch (e) {
      // Minimal fallback YAML serializer
      const serialize = (value, indent = 0) => {
        const pad = ' '.repeat(indent);
        if (Array.isArray(value)) {
          return value.map(v => `${pad}- ${serialize(v, indent + 2).trimStart()}`).join('\n');
        } else if (isObject(value)) {
          return Object.entries(value)
            .map(([k, v]) => `${pad}${k}: ${serialize(v, indent + 2).trimStart()}`)
            .join('\n');
        } else {
          return `${value}`;
        }
      };
      return serialize(data);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
algis profile image
Algis

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

Collapse
 
david_burton_e7d4fbc8c13b profile image
David Burton

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

Collapse
 
richardevcom profile image
richardevcom

I'll just leave this here for people still taking this seriously 👀

Collapse
 
gauravchandra profile image
Gaurav Chandra

How it is different from TONL github.com/tonl-dev/tonl ?

Collapse
 
salient-pr profile image
Salient PR

Thank you for the article)

Collapse
 
ketan_gupta_8e07aabce5378 profile image
Ketan Gupta

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 🛠️

Collapse
 
jensenholt profile image
JensenHolt

Thanks for sharing.

Collapse
 
kskitek profile image
kskitek

Why would you pick TOON over CSV?

Collapse
 
now_raj profile image
Raj Tejaswee

Thanks for the article. Had read many articles, but this one explains in much better way!

Collapse
 
maikidev profile image
MaikiDev

Thorough resource on how to use TOON, thanks! I've made a free converter from JSON to TOON to showcase the library functionality.