API Documentation

JSON to TOON Converter REST API

← Back to Converter

Overview

The JSON to TOON API provides a simple REST endpoint for converting between JSON and TOON (Token-Oriented Object Notation) formats. TOON achieves ~40% token reduction compared to JSON, making it ideal for LLM prompts and AI applications.

Base URL:
https://jsontotoon.io/api/v1
🔑 Authentication Required
The programmatic API (/v1/convert) requires an API key. Get your free API key here.

Note: The web interface at jsontotoon.io is free to use without an API key.
✓ Free API Keys - No cost, no expiration
✓ CORS Enabled - Works from any domain
✓ No Rate Limits - Use as needed

Endpoint

Convert Between JSON and TOON

POST /convert

Converts data between JSON and TOON formats in either direction.

Request Parameters

Parameter Type Required Description
input string Required The JSON string or TOON string to convert
direction string Required Either "json_to_toon" or "toon_to_json"

Response

Success Response (200 OK):

JSON { "output": "<converted string>", "direction": "json_to_toon" }

Error Response (400 Bad Request):

JSON { "error": "Error message describing what went wrong" }

Examples

JSON to TOON Conversion

Bash curl -X POST https://jsontotoon.io/api/convert \ -H "Content-Type: application/json" \ -d '{ "input": "{\"name\": \"Alice\", \"age\": 30}", "direction": "json_to_toon" }'
JavaScript const response = await fetch('https://jsontotoon.io/api/convert', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ input: JSON.stringify({ name: 'Alice', age: 30 }), direction: 'json_to_toon' }) }); const data = await response.json(); console.log(data.output); // Output: name: Alice\nage: 30
Python import requests import json url = 'https://jsontotoon.io/api/convert' payload = { 'input': json.dumps({'name': 'Alice', 'age': 30}), 'direction': 'json_to_toon' } response = requests.post(url, json=payload) data = response.json() print(data['output']) # Output: name: Alice\nage: 30
Go package main import ( "bytes" "encoding/json" "fmt" "net/http" ) func main() { url := "https://jsontotoon.io/api/convert" payload := map[string]string{ "input": `{"name": "Alice", "age": 30}`, "direction": "json_to_toon", } jsonData, _ := json.Marshal(payload) resp, _ := http.Post(url, "application/json", bytes.NewBuffer(jsonData)) var result map[string]interface{} json.NewDecoder(resp.Body).Decode(&result) fmt.Println(result["output"]) // Output: name: Alice\nage: 30 }

TOON to JSON Conversion

Bash curl -X POST https://jsontotoon.io/api/convert \ -H "Content-Type: application/json" \ -d '{ "input": "name: Alice\nage: 30", "direction": "toon_to_json" }'
JavaScript const response = await fetch('https://jsontotoon.io/api/convert', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ input: 'name: Alice\nage: 30', direction: 'toon_to_json' }) }); const data = await response.json(); const jsonObj = JSON.parse(data.output); console.log(jsonObj); // Output: { name: 'Alice', age: 30 }
Python import requests import json url = 'https://jsontotoon.io/api/convert' payload = { 'input': 'name: Alice\nage: 30', 'direction': 'toon_to_json' } response = requests.post(url, json=payload) data = response.json() json_obj = json.loads(data['output']) print(json_obj) # Output: {'name': 'Alice', 'age': 30}
Go package main import ( "bytes" "encoding/json" "fmt" "net/http" ) func main() { url := "https://jsontotoon.io/api/convert" payload := map[string]string{ "input": "name: Alice\\nage: 30", "direction": "toon_to_json", } jsonData, _ := json.Marshal(payload) resp, _ := http.Post(url, "application/json", bytes.NewBuffer(jsonData)) var result map[string]interface{} json.NewDecoder(resp.Body).Decode(&result) fmt.Println(result["output"]) // Output: {"name": "Alice", "age": 30} }

Error Handling

Common Errors

Invalid JSON (400):

{ "error": "Invalid JSON: Expecting property name enclosed in double quotes" }

Invalid Direction (400):

{ "error": "Invalid direction: wrong_value. Use \"json_to_toon\" or \"toon_to_json\"" }

Missing Input (400):

{ "error": "Missing required field: input" }

TOON Parsing Error (400):

{ "error": "TOON parsing error: Invalid TOON syntax at line 2" }

Best Practices

⚠ Input Validation
Always validate and sanitize your input before sending to the API. The API will reject invalid JSON or TOON syntax.

Tips for Optimal Usage

  • Escape JSON properly: When sending JSON as a string, ensure it's properly escaped
  • Handle errors gracefully: Always check response status and handle error cases
  • Use TOON for LLMs: TOON is optimized for LLM inputs - use it when token count matters
  • Preserve newlines: TOON format uses newlines for structure - preserve them in your strings
  • Test with simple data first: Start with simple objects before converting complex nested structures

When to Use TOON

  • ✅ Sending data to LLM APIs (Claude, GPT, etc.)
  • ✅ Tabular data with uniform structure
  • ✅ Reducing prompt token costs
  • ✅ Human-readable data in prompts

When to Stick with JSON

  • ❌ Standard REST API responses
  • ❌ Deeply nested irregular structures
  • ❌ When token count doesn't matter
  • ❌ Systems that require strict JSON

About TOON Format

TOON (Token-Oriented Object Notation) is a compact, human-readable format designed specifically for Large Language Model inputs. It combines YAML-style indentation with CSV-like tabular layouts for uniform arrays, optimizing for the way LLMs actually tokenize text.

The Problem TOON Solves

When working with LLM APIs like Claude, GPT-4, or Gemini, you pay per token. JSON is great for machines, but it's incredibly wasteful for tokens—all those curly braces, quotes, and commas add up fast. For applications sending large datasets in prompts (think RAG systems, data analysis agents, or multi-shot examples), token costs can spiral quickly.

TOON was created to solve this exact problem. By removing unnecessary syntax while maintaining structure and readability, it achieves significant token reduction without sacrificing data fidelity.

How It Works

JSON to TOON conversion example:

JSON (125 tokens) [ {"name": "Alice", "age": 30, "city": "NYC"}, {"name": "Bob", "age": 25, "city": "LA"}, {"name": "Carol", "age": 35, "city": "Chicago"} ]
TOON (68 tokens - 46% reduction) name, age, city Alice, 30, NYC Bob, 25, LA Carol, 35, Chicago

For uniform arrays (like database rows or API responses), TOON uses a CSV-style header format. For nested objects, it uses YAML-style indentation. The format is deterministic—the same JSON always produces the same TOON output, and round-trip conversion is lossless.

Key Benefits

  • ~40% Token Reduction: Measured across real-world datasets—your API bills will thank you
  • Lossless Conversion: Deterministic round-trip conversion with JSON. No data loss, ever.
  • Human Readable: More readable than JSON for tabular data. Your prompts become clearer.
  • LLM Optimized: Designed specifically for how modern tokenizers work (BPE, WordPiece, etc.)
  • Drop-in Replacement: Convert at the edges—your application logic stays unchanged

Real-World Use Cases

  • RAG Systems: Include more context in your prompts without hitting token limits
  • Data Analysis Agents: Send large datasets to LLMs for analysis at lower cost
  • Multi-shot Learning: Fit more examples in your few-shot prompts
  • Structured Outputs: LLMs can generate TOON outputs that are easier to parse reliably
  • Logging & Debugging: More readable logs when you're examining LLM inputs/outputs

Technical Details

TOON uses indentation-based nesting (like YAML) for objects and a header-based format (like CSV) for arrays of uniform objects. The parser is deterministic and handles edge cases like nested arrays, mixed-type arrays, null values, and special characters. Unlike YAML, TOON has a much simpler specification focused exclusively on JSON compatibility—no weird type coercion or ambiguous syntax.

Parser Implementation: The conversion logic is written in Python using standard libraries. It's fast, well-tested, and handles malformed input gracefully. Check the specification for the complete grammar.

Learn more: TOON Format Specification

Support & Resources

TOON Specification: github.com/toon-format/toon

Need Help?
Check the TOON format documentation for detailed examples and the complete specification.