Skip to main content
C
CodeUtil

Why I Switched from YAML to TOML (And When I Still Use YAML)

YAML indentation bugs cost me a Friday night once. Now I use TOML for most configs. Here's my practical guide to picking the right format.

2024-08-147 min
Related toolTOML Formatter

Use the tool alongside this guide for hands-on practice.

The config format that doesn't bite you

I used to think YAML was fine. Then one Friday night, a misaligned space in a CI config took down our staging environment. That's when I started looking at TOML seriously.

TOML (Tom's Obvious Minimal Language) was created by GitHub's co-founder specifically to avoid this pain. No whitespace sensitivity, explicit syntax, and it's now the standard for Rust and Python projects. Here's when I use each format.

TOML vs YAML vs JSON comparison

Each configuration format has distinct strengths and weaknesses. Understanding these helps you choose the right format for your project.

  • TOML: Explicit syntax, no whitespace sensitivity, supports comments, great for config files
  • YAML: Human-friendly, supports complex structures, whitespace-sensitive (error-prone)
  • JSON: Universal interchange format, strict syntax, no comments, verbose for config

When to use TOML

TOML excels for application configuration where readability and explicit syntax matter. Its lack of whitespace sensitivity makes it less error-prone than YAML.

  • Package manifests (Cargo.toml, pyproject.toml)
  • Application configuration files
  • CI/CD configuration (netlify.toml, hugo.toml)
  • Settings that humans will edit frequently

When to use YAML

YAML is ideal for complex nested configurations and data serialization where its features like anchors and aliases provide value.

  • Kubernetes manifests and Helm charts
  • Docker Compose files
  • CI/CD pipelines (GitHub Actions, GitLab CI)
  • Complex data structures with references

When to use JSON

JSON is the standard for data interchange between systems. Use it when your configuration needs to be consumed by APIs or generated programmatically.

  • API responses and requests
  • package.json and similar manifests
  • Machine-generated configuration
  • Cross-language data exchange

Converting between formats

Converting between TOML, YAML, and JSON is straightforward since they all represent similar data structures. However, some features are format-specific.

  • Comments are lost when converting to JSON
  • YAML anchors/aliases cannot be represented in TOML or JSON
  • TOML dates become strings in JSON
  • Nested structures may look different after conversion

TOML syntax quick reference

TOML uses a simple key-value syntax with tables for grouping related settings. Square brackets define table headers, and double brackets create arrays of tables.

FAQ

Is TOML better than YAML?

For configs humans edit? Yes. TOML has no whitespace sensitivity, so you can't break things with a misplaced space. YAML is better for deeply nested stuff like Kubernetes manifests.

Can I use TOML for Kubernetes?

Stick with YAML for Kubernetes. The entire ecosystem uses it. Fighting that convention isn't worth it.

Why does Rust use TOML?

Unambiguous syntax. No whitespace gotchas. Supports comments. Rust wanted a config format that wouldn't cause debugging nightmares.

How do I validate a Cargo.toml file?

Paste it into our TOML formatter. It validates syntax and highlights errors instantly.

Martin Šikula

Founder of CodeUtil. Web developer building tools I actually use. When I'm not coding, I experiment with productivity techniques (with mixed success).

Related articles