Converting JSON to TOON is straightforward once you understand the rules. This tutorial walks you through every step with practical code examples.
TOON Conversion Rules
The conversion follows these simple rules:
- Replace curly braces with indentation
- Convert uniform arrays to tabular format
- Remove unnecessary quotes from strings
- Replace colons with spaces for object keys
Rule 1: Objects Without Braces
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 Code
function objectToToon(obj, indent = '') {
let result = '';
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
// Nested object - add key and recurse with indentation
result += indent + key + '\n';
result += objectToToon(value, indent + ' ');
} else if (Array.isArray(value)) {
// Handle arrays separately
result += indent + key + ' ' + arrayToToon(value, indent) + '\n';
} else {
// Simple key-value
result += indent + key + ': ' + formatValue(value) + '\n';
}
}
return result;
}
function formatValue(val) {
if (val === null) return 'null';
if (typeof val === 'boolean') return val.toString();
if (typeof val === 'number') return val.toString();
// Quote strings only if they contain special characters
if (/[,:\n\r]/.test(val)) return '"' + val + '"';
return val;
}
// Example
const product = {
name: "Product A",
price: 29.99,
inStock: true,
category: "Electronics"
};
console.log(objectToToon(product));
// Output:
// name: Product A
// price: 29.99
// inStock: true
// category: Electronics
Rule 2: Tabular Arrays
When all items in an array share the same structure, convert to table format:
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
def array_to_toon(key, arr):
"""Convert uniform array to TOON tabular format"""
if not arr or not isinstance(arr[0], dict):
return key + " [" + str(len(arr)) + "]\n" + "\n".join("- " + str(v) for v in arr)
# Check if uniform (all items have same keys)
first_keys = set(arr[0].keys())
is_uniform = all(set(item.keys()) == first_keys for item in arr)
if not is_uniform:
return key + " [" + str(len(arr)) + "]\n" + "\n".join("- " + str(item) for item in arr)
# Create tabular format
fields = list(arr[0].keys())
header = key + " [" + str(len(arr)) + "] {" + ', '.join(fields) + "}"
rows = []
for item in arr:
values = [format_value(item[f]) for f in fields]
rows.append(", ".join(values))
return header + "\n" + "\n".join(rows)
def format_value(val):
"""Format a value for TOON"""
if val is None:
return "null"
if isinstance(val, bool):
return "true" if val else "false"
if isinstance(val, (int, float)):
return str(val)
# Quote if contains comma or newline
if "," in str(val) or "\n" in str(val):
return '"' + str(val) + '"'
return str(val)
# Example
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}
]
print(array_to_toon("orders", orders))
# Output:
# orders [3] {orderId, product, qty, price}
# ORD001, Laptop, 1, 999.99
# ORD002, Mouse, 2, 29.99
# ORD003, Keyboard, 1, 79.99
Rule 3: Nested Objects
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
- Forgetting array length - Always include [N]
- Inconsistent indentation - Use consistent 2-space indentation
- Not quoting special characters - Values with commas need quotes
- Missing field headers - Tabular arrays require {fields}
🔧 Try Our Free TOON Converter
Convert your JSON to TOON format instantly and see your token savings in real-time!
⚡ Open TOON Converter