Data Formats

JSON to TOON Conversion: Step-by-Step Tutorial with Examples

📅 December 10, 2025 ⏱️ 3 min read 👁️ 117 views 🏷️ Data Formats

Converting JSON to TOON becomes simple once the core patterns are clear. I struggled at first because I treated it like a strict one to one syntax replacement. In practice, TOON is more about structure than symbols. After a few failed conversions and indentation mistakes, the rules started to feel natural.

TOON Conversion Rules

Every JSON to TOON conversion follows a small set of predictable rules. Missing even one usually leads to parsing issues later.

  1. Curly braces are replaced by indentation
  2. Uniform arrays are expressed as tables
  3. Quotes are removed unless the value requires them
  4. Colons are simplified into key value spacing

Rule 1: Objects Without Braces

This is where most developers trip up initially. I kept leaving stray braces in my output out of habit. TOON relies entirely on indentation to express hierarchy.

JSON Input

{
  "name": "Product A",
  "price": 29.99,
  "inStock": true,
  "category": "Electronics"
}

TOON Output

name: Product A
price: 29.99
inStock: true
category: Electronics

JavaScript Conversion Logic

My earliest errors here came from invalid JSON inputs. Running the source through https://jsonformatterspro.com before conversion eliminated those problems.

function objectToToon(obj, indent = '') {
  let out = '';
  for (const key in obj) {
    const val = obj[key];
    if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
      out += indent + key + '\n';
      out += objectToToon(val, indent + '  ');
    } else if (Array.isArray(val)) {
      out += indent + key + ' ' + arrayToToon(val) + '\n';
    } else {
      out += indent + key + ': ' + formatValue(val) + '\n';
    }
  }
  return out;
}

function formatValue(val) {
  if (val === null) return 'null';
  if (typeof val === 'boolean' || typeof val === 'number') return String(val);
  if (/[,:\n\r]/.test(val)) return '"' + val + '"';
  return val;
}

Rule 2: Tabular Arrays

Uniform arrays are where TOON shines. My main mistake here was assuming all arrays qualify. If even one object has a missing key, the table breaks.

JSON Input

{
  "orders": [
    {"orderId": "ORD001", "product": "Laptop", "qty": 1, "price": 999.99},
    {"orderId": "ORD002", "product": "Mouse", "qty": 2, "price": 29.99},
    {"orderId": "ORD003", "product": "Keyboard", "qty": 1, "price": 79.99}
  ]
}

TOON Output

orders [3] {orderId, product, qty, price}
ORD001, Laptop, 1, 999.99
ORD002, Mouse, 2, 29.99
ORD003, Keyboard, 1, 79.99

Python Implementation

Early versions of this function failed silently when array lengths mismatched headers. Adding uniformity checks prevented bad output.

def array_to_toon(key, arr):
    if not arr or not isinstance(arr[0], dict):
        return key + " [" + str(len(arr)) + "]"
    keys = list(arr[0].keys())
    if not all(list(item.keys()) == keys for item in arr):
        return key + " [" + str(len(arr)) + "]"
    header = key + " [" + str(len(arr)) + "] {" + ", ".join(keys) + "}"
    rows = []
    for item in arr:
        rows.append(", ".join(format_value(item[k]) for k in keys))
    return header + "\n" + "\n".join(rows)

def format_value(val):
    if val is None:
        return "null"
    if isinstance(val, bool):
        return "true" if val else "false"
    if isinstance(val, (int, float)):
        return str(val)
    if "," in str(val) or "\n" in str(val):
        return '"' + str(val) + '"'
    return str(val)

Rule 3: Nested Objects

Nested objects exposed most of my indentation errors. One extra space can completely change meaning, so consistency matters more than speed.

JSON Input

{
  "user": {
    "profile": {
      "name": "Jane Doe",
      "email": "jane@example.com"
    },
    "settings": {
      "theme": "dark",
      "notifications": true
    }
  }
}

TOON Output

user
  profile
    name: Jane Doe
    email: jane@example.com
  settings
    theme: dark
    notifications: true

Common Pitfalls I Encountered

  1. Missing array counts which causes parsing failures
  2. Uneven indentation leading to broken hierarchy
  3. Unquoted commas in values corrupting rows
  4. Non uniform objects inside tabular arrays

🔧 Try Our Free TOON Converter

Convert your JSON to TOON format instantly and see your token savings in real-time!

⚡ Open TOON Converter
🏷️ Tags:
json to toon conversion tutorial javascript python code examples

📚 Related Articles