How do I convert JSON to TOON format?

Converting JSON to TOON format involves transforming the hierarchical structure while removing syntactic overhead.

Basic conversion rules:

  1. Remove all quotation marks from keys and string values unless they contain colons, newlines, or start with whitespace
  2. Remove opening and closing braces {} for objects
  3. Remove opening and closing brackets [] for arrays
  4. Use tab characters (or spaces) for indentation to represent nesting levels
  5. Keep colons between keys and values
  6. Use newlines to separate items instead of commas
  7. For arrays, list items on separate lines at the same indentation level, optionally with a dash prefix for clarity

Example:

JSON: {"user": {"name": "Alice", "age": 30, "tags": ["developer", "scraper"]}}

Converts to TOON:

user:
	name: Alice
	age: 30
	tags:
		developer
		scraper

Special cases:

  • Numbers and booleans remain unchanged
  • Null values can be represented as null or omitted
  • Empty objects become just the key with a colon
  • Strings with special characters (colons, newlines, leading/trailing spaces) should keep quotes
  • Arrays of objects follow the same indentation pattern as nested objects

You can use our JSON to TOON converter tool which automatically handles these transformations, shows token count comparisons, and validates the output. For programmatic conversion, implement a recursive parser that walks the JSON structure and builds TOON output with proper indentation.

Related Questions