Skip to content

JSON vs YAML vs TOML: When to Use Which

Sep 2, 2026 · Formats & Standards

Use JSON for machine-to-machine data exchange such as APIs, TOML for human-edited configuration that is mostly flat, and YAML for deeply nested configuration where readability matters more than avoiding whitespace pitfalls. All three represent the same underlying idea, a tree of keys, values, lists, and nested tables, but they make very different trade-offs between strictness, readability, and surprise. This article compares them fairly, including the gotchas each one hides.

What do these three formats have in common?

All three encode the same data model: scalars such as strings, numbers, and booleans, plus two containers, an ordered list and a key/value mapping. That shared model is why converting between them is usually straightforward, and why a JSON to YAML converter can round-trip most documents. Where they differ is in the surface syntax you type and in how forgiving or strict each parser is. Here is the same small configuration expressed three ways.

JSON:

{
  "name": "web",
  "port": 8080,
  "debug": false,
  "hosts": ["a.example", "b.example"]
}

YAML:

name: web
port: 8080
debug: false
hosts:
  - a.example
  - b.example

TOML:

name = "web"
port = 8080
debug = false
hosts = ["a.example", "b.example"]

Same data, three grammars. Notice JSON’s braces and quotes, YAML’s indentation and dashes, and TOML’s key = value lines that look like an INI file.

How do the three formats compare at a glance?

This table summarises the practical differences that actually affect a decision:

Aspect JSON YAML TOML
Primary use Data exchange, APIs Config, especially nested Config, mostly flat
Comments No Yes (#) Yes (#)
Structure marker Braces and brackets Indentation key = value and [tables]
Whitespace sensitive No Yes No
Trailing commas No N/A Allowed in arrays
Human-friendliness Moderate High until deeply nested High for flat config
Type coercion surprises None Many Few
Native date type No Yes Yes
Ubiquity in tooling Universal Very wide Growing

When should you use JSON?

Reach for JSON whenever software talks to software. It is the default body format for HTTP APIs, it is supported natively in effectively every programming language, and its strictness is exactly what you want on the wire: there is one way to write each value, no comments to strip, and no ambiguity for a parser to resolve. That same strictness makes JSON tedious to edit by hand. It has no comments, it demands double quotes around every key and string, and it forbids trailing commas, so a config file maintained by people accumulates friction. JSON is the right choice for request and response payloads, for data stored and shipped between services, and for anything generated by a program rather than typed by a person.

When should you use YAML?

Reach for YAML when configuration is deeply nested and you want it to read almost like an outline. Kubernetes manifests, CI pipelines, and Ansible playbooks favour YAML because indentation expresses hierarchy without a pile of closing braces, and because comments let you annotate why a setting exists. The cost is that YAML is the least predictable of the three. Because it uses indentation for structure, a single misaligned space or a stray tab changes the meaning or breaks the parse, and tabs are forbidden as indentation entirely. It also performs aggressive type coercion on unquoted scalars, which produces genuine bugs covered in the gotchas section below. YAML rewards you with readability for large nested documents and punishes careless whitespace and unquoted values.

When should you use TOML?

Reach for TOML when you want a config file that is obvious to read and hard to get wrong, and the structure is flat or only moderately nested. TOML was designed specifically for configuration, and it shows: values are unambiguous, strings are quoted so there is no guessing about types, dates are a first-class type, and sections are declared with clear [table] headers. It is the format behind Rust’s Cargo and Python’s pyproject.toml. Where TOML becomes awkward is deep nesting: expressing several levels of nested tables and arrays-of-tables gets verbose and harder to follow than the equivalent YAML. TOML is the right choice for application and project configuration that a human edits and that does not nest very deeply.

What gotchas should you watch for in each format?

Each format hides traps, and knowing them prevents most real-world bugs.

What are YAML’s biggest traps?

YAML’s flexibility is the source of its surprises. The most notorious is implicit type coercion of unquoted scalars:

  • The Norway problem. Under YAML 1.1 rules, the unquoted values yes, no, on, off, true, and false all become booleans. A list of country codes that includes NO for Norway silently turns into false. Quote the value as "NO" to keep it a string.
  • Numbers that were meant to be strings. A version like 1.10 parses as the number 1.1, losing the trailing zero, and a ZIP code like 01234 may be read as an integer or as octal. Quote anything that should stay text.
  • Whitespace and tabs. Indentation defines structure, so inconsistent spacing changes nesting, and a tab used for indentation is a hard error.
  • The colon-space rule. A mapping needs a space after the colon; key:value is not a key/value pair.
  • Anchors and merge keys. YAML lets you define an anchor and reuse it elsewhere, which is powerful but adds a layer of indirection that makes a document harder to read and, in some parsers, has been a source of denial-of-service and security issues when references expand explosively.

None of these are bugs in YAML itself; they are the price of a format that tries hard to guess what you meant. The defensive habit that removes almost all of them is simple: quote any scalar whose type is not obvious, and keep indentation consistent with spaces only.

What are JSON’s limitations?

JSON’s traps are omissions rather than surprises. There are no comments, so teams resort to fake "_comment" keys. There are no trailing commas, which makes reordering fields error-prone. There is no date type, so dates travel as strings by convention. And every string and key must use double quotes, never single, which trips up anyone copying from a JavaScript object literal.

What are TOML’s rough edges?

TOML is the most predictable of the three, but deep nesting is its weak spot. Nested tables use dotted headers like [servers.web.limits], and lists of nested objects use the double-bracket [[table]] array-of-tables syntax, both of which become hard to scan once you go more than a couple of levels deep. TOML also requires strings to be quoted, unlike YAML’s bare scalars, which is slightly more typing but removes an entire class of ambiguity.

How do you convert between them safely?

Convert between these formats in a tool that runs locally in your browser, because config and API files frequently contain secrets. Since all three share the same data model, conversion is mechanical for the common cases, and a client-side JSON to YAML converter handles round-trips without sending your content to a server. That local-only property matters here more than usual: configuration files are exactly where database passwords, API keys, and tokens live, and those should never be pasted into a remote converter. Convert on your own machine, keep the data on it, and you get the readability benefits of switching formats without the exposure.

There is no single winner. JSON wins for APIs and machine exchange, TOML wins for readable flat configuration, and YAML wins for deep nested configuration where you accept its whitespace and coercion quirks in return for a clean, comment-friendly layout. Pick by the job in front of you rather than by habit.

Frequently asked questions

Is YAML a superset of JSON?

Yes. YAML 1.2 is a strict superset of JSON, so any valid JSON document is also valid YAML and can be parsed by a compliant YAML parser. The reverse is not true, since YAML has features JSON lacks.

Why is YAML whitespace sensitive?

YAML uses indentation to express nesting instead of braces or brackets, so the number of leading spaces defines structure. Inconsistent indentation, or any tab character, changes the meaning or causes a parse error.

Does JSON support comments?

No. JSON has no comment syntax, which is a common reason teams choose YAML or TOML for human-edited configuration files where explanatory comments are valuable.

What is the Norway problem in YAML?

It is a type-coercion surprise where YAML 1.1 parsers read the unquoted value no as the boolean false, so a country code like NO becomes false. Quoting the value as "no" prevents it.

Which format is best for configuration files?

TOML is designed for configuration and stays readable and unambiguous for flat and moderately nested settings. YAML suits deeply nested config, and JSON suits machine-to-machine data exchange more than hand editing.