DataSwitch Pro Private, in-browser data converter

TOML vs YAML vs JSON for Configuration Files

By the DataSwitch Pro team · · 4 min read

Quick answer

TOML is explicit and unambiguous — every string is quoted, types are clear, comments are allowed — which makes it ideal for flat or moderately nested settings such as pyproject.toml and Cargo.toml. YAML is the most compact and readable for deeply nested structures (Kubernetes, Docker Compose, CI pipelines) but has indentation and implicit-typing pitfalls. JSON is strict and universal but has no comments, so it suits files written by programs, such as package.json lock data and API payloads, better than hand-edited config.

Most projects end up with all three: a package.json, a docker-compose.yml and a pyproject.toml or Cargo.toml. Each format was designed with different trade-offs, and knowing them helps you pick the right one for a new file — and avoid the classic mistakes in each.

The same settings in three formats

TOML

# Application settings
[server]
host = "0.0.0.0"
port = 8080

[database]
url = "postgres://localhost/app"
pool_size = 10

[[workers]]
name = "emails"
threads = 2

YAML

# Application settings
server:
  host: 0.0.0.0
  port: 8080
database:
  url: postgres://localhost/app
  pool_size: 10
workers:
  - name: emails
    threads: 2

JSON

{
  "server": { "host": "0.0.0.0", "port": 8080 },
  "database": { "url": "postgres://localhost/app", "pool_size": 10 },
  "workers": [ { "name": "emails", "threads": 2 } ]
}

Comparison

Where each one is the norm

When a tool has a convention, follow it. The choice only really matters for new files you design yourself.

How to choose for a new file

  1. Mostly written by programs? Use JSON.
  2. Edited by people, mostly flat sections of settings? Use TOML: it is hard to get wrong and easy to read.
  3. Edited by people, deeply nested or long lists of structured items? Use YAML, with quoting rules agreed in the team and a linter in CI.

Common pitfalls

Converting between them

Moving a configuration from one format to another is mostly mechanical: JSON to TOML turns nested objects into [section] tables, and TOML to JSON shows exactly how a TOML file is structured — useful when arrays of tables are confusing. Watch for the differences above: null values have no TOML equivalent, comments are lost when converting to JSON, and dates become plain strings in JSON. For a closer look at the other pair, read YAML vs JSON.

Try it on your own file. Free, private, and processed in your browser.

Frequently asked questions

Does TOML support null?

No. Omit the key, or use an empty string or another sentinel value that your program understands.

Is TOML better than YAML?

For simple, flat configuration, TOML is usually safer because it has no indentation or implicit-typing surprises. For deep nesting, YAML is more compact.

Can I put comments in package.json?

No. npm reads strict JSON. Some tools accept a "//" key as a workaround, but it is just data.

Which format does pyproject.toml use?

TOML, as standardised by the Python packaging specifications (PEP 518 and PEP 621).

More guides