JSON, YAML and TOML Compared for Configuration Files
Compare JSON, YAML and TOML for configuration files, with syntax examples, strengths, pitfalls and guidance on which format fits which project.
Nearly every application needs a configuration file: a place to store settings such as ports, paths, feature switches and lists of options. Three text formats dominate this job: JSON, YAML and TOML. All three express the same kinds of data, but they feel very different to write and to maintain.
Understanding their strengths and weak spots helps you choose well, and helps you avoid the classic mistakes that each one invites. We will use the same small configuration in each format so the comparison is fair.
JSON
JSON, short for JavaScript Object Notation, is the most widely supported of the three. Nearly every programming language can read and write it, and it is the standard format for web APIs.
{
"name": "demo",
"port": 8080,
"debug": false,
"tags": ["web", "internal"],
"database": {
"host": "localhost",
"timeout": 30
}
}
Strengths. The grammar is tiny and strict, so parsers are fast and consistent. There are very few surprises, and tools for formatting and validating it are everywhere.
Weaknesses. JSON does not allow comments, which is a real handicap for a file that humans edit and need to explain. It also requires double quotes around keys, rejects a trailing comma after the last item, and gets noisy with braces and quotes. A single missed comma makes the entire file invalid.
Best for. Data exchanged between programs, and configuration that is mostly generated or edited by tools rather than by hand.
YAML
YAML uses indentation instead of braces, so it looks clean and light. It is common in infrastructure and automation files, such as container and pipeline definitions.
name: demo
port: 8080
debug: false
tags:
- web
- internal
database:
host: localhost
timeout: 30 # seconds
Strengths. It reads easily, supports comments, and handles nested data and long text blocks gracefully. It also allows anchors and aliases to reuse a piece of data in several places.
Weaknesses. Indentation is part of the syntax, and a wrong number of spaces can quietly change the meaning of the file. Tabs are not allowed for indentation. The specification is large, and some behaviours surprise people. The best known example is implicit typing: in some YAML versions, an unquoted value such as no, off or yes is read as a boolean, and something like 1.10 is read as a number, which drops the trailing zero. A country code NO for Norway may turn into false. Quote strings whenever the type is in doubt.
Security matters too. Some YAML libraries can construct arbitrary objects from a file, so use the safe loading function of your language when you read files you did not write.
Best for. Human edited files with deep nesting, where comments and readability matter and the team has good validation in place.
TOML
TOML, which stands for Tom's Obvious, Minimal Language, was designed specifically for configuration. It looks like an enhanced version of the classic INI file.
name = "demo"
port = 8080
debug = false
tags = ["web", "internal"]
[database]
host = "localhost"
timeout = 30 # seconds
Strengths. The meaning of each value is explicit. Strings are always quoted, numbers and booleans are not, and dates are a native type. It supports comments, and there is no significant indentation to get wrong. Files are easy to read and diff.
Weaknesses. Deeply nested data becomes awkward, with long section headers such as [servers.alpha.network]. Lists of tables use a double bracket syntax that newcomers find unfamiliar. Support is wide but not as universal as JSON, although it has grown rapidly and is the standard for several popular tools.
Best for. Application settings and project metadata that people edit by hand and that are mostly flat or shallow.
Side by side
- Comments. JSON no. YAML yes. TOML yes.
- Readability for humans. JSON fair, YAML high but fragile, TOML high.
- Deep nesting. JSON and YAML handle it well. TOML is clumsier.
- Strictness. JSON is strict, TOML is strict, YAML is flexible, which also means error prone.
- Ecosystem. JSON is universal, YAML is very common in tooling, TOML is common in newer projects.
- Data types. JSON has few. TOML adds dates. YAML guesses types unless you quote them.
How to choose
- If the file is exchanged with other programs or web services, use JSON.
- If humans edit it often and it is shallow, use TOML.
- If the surrounding tool already expects YAML, such as a deployment platform, follow the convention rather than fight it.
- If your team makes frequent whitespace mistakes, avoid YAML for critical settings or add automatic checks.
Whichever you choose, validate files before use. Run a linter or a schema check in your build, document the allowed keys, and keep an example file in the repository. A clear error at startup is far better than a program that quietly runs with wrong settings.
Frequently Asked Questions
Can I convert between JSON, YAML and TOML?
Usually yes, because they model similar data. Many free command line and online tools convert them. Check the result, since comments are lost when moving from YAML or TOML to JSON, and some types may change.
Why is YAML so popular if it has so many pitfalls?
It reads well and supports comments, which suits files that people maintain. The pitfalls are manageable with quoting, linting and safe loaders, and many tools adopted it early and kept it for compatibility.
Is JSON with comments a good idea?
Some tools accept a variant that allows comments, but it is not standard JSON, and a strict parser will reject it. If you need comments and want a standard format, TOML or YAML is a safer choice.