YAML vs JSON: When to Use Each Format

Use YAML for configuration files humans write and maintain — it supports comments, requires less punctuation, and is easier to read. Use JSON for APIs and data exchange — it parses faster, has a simpler spec, and is supported natively in every browser and language. YAML 1.2 is a strict superset of JSON, so any valid JSON is also valid YAML.

Try it directly in your browser:

Convert YAML to JSON →

The answer comes down to who reads the file and how frequently.

Quick comparison

FeatureYAMLJSON
Human readabilityHigh — minimal punctuation, indentation-basedMedium — curly braces and quotes add visual noise
CommentsYes (# comment)No
Multiline stringsYes (block scalars | and >)Requires \n escape sequences
Parse speedSlower — complex specFaster — simpler spec
Whitespace sensitivityYes — indentation errors break the documentNo — whitespace is ignored
Browser supportNo native parserNative (JSON.parse)
Spec complexityHigh (80+ pages)Low (a few pages)
Anchors / aliasesYes (reuse values with & / *)No
Superset relationshipYAML 1.2 is a superset of JSONJSON is a subset of YAML 1.2

The same data in both formats

JSON:

{
  "server": {
    "host": "api.example.com",
    "port": 8080,
    "tls": true
  },
  "features": ["logging", "metrics", "tracing"]
}

YAML equivalent:

server:
  host: api.example.com
  port: 8080
  tls: true
features:
  - logging
  - metrics
  - tracing

The YAML version is shorter and easier to scan. It has no commas, no curly braces, and no quotation marks. The trade-off is that a single indentation mistake breaks the whole document.

When to use YAML

Configuration files humans maintain

YAML's main strength is readability. When a developer has to open a file, read it, understand it, and edit it — YAML reduces friction. This is why it dominates in:

When comments matter

JSON has no comment syntax. If you need to explain a configuration choice, disable a block temporarily, or leave a note for future developers, YAML is the only option. This alone makes YAML the right choice for complex config files.

# Production server config
# Do not increase replicas above 10 without checking DB connection pool limits
server:
  replicas: 5  # Scaled up from 3 on 2026-03-01
  timeout: 30

When you have multiline text

YAML block scalars make embedding multiline text natural:

message: |
  Hello,
  This is a multiline
  message without escape sequences.

In JSON, the same value would be "message": "Hello,\nThis is a multiline\nmessage without escape sequences." — much harder to read and edit.

When to use JSON

APIs and data exchange

JSON was designed for data transport. When data moves between a server and a browser, between two services, or in a message queue, JSON is the right format because:

When the consumer is a machine, not a human

If no human ever reads the raw format — the data goes straight from one API to another, or is stored in a database — JSON's compactness and parse speed are advantages with no downside.

When you need browser compatibility

Browsers have no built-in YAML parser. If your frontend needs to consume structured data from a fetch() call or a <script> tag, JSON is the only practical choice without adding a library.

Database document storage

MongoDB, CouchDB, PostgreSQL's JSONB, and most NoSQL databases store and query JSON natively. YAML has no equivalent storage support.

The "YAML is just for config, JSON is for APIs" rule

A practical rule that works in most cases:

There are exceptions — some tools use JSON for config (package.json, tsconfig.json) and some APIs return YAML — but the rule covers the overwhelming majority of real-world cases.

Which should you use? A decision guide

SituationUseReason
REST API request / response bodyJSONUniversal support, native browser parsing, fast
Kubernetes / Helm manifestYAMLEcosystem standard, comments essential
Docker Compose fileYAMLEcosystem standard
GitHub Actions / GitLab CI workflowYAMLEcosystem standard, anchors reduce repetition
Ansible playbookYAMLEcosystem standard
Application config (human-maintained)YAMLComments, readability, no trailing-comma bugs
Database serializationJSONNative DB support (PostgreSQL jsonb, MongoDB)
Message queue payloadsJSONSpeed and universal consumer support
package.json / tsconfig.jsonJSONNode.js ecosystem convention
OpenAPI / Swagger specEitherBoth supported; YAML preferred for hand-authoring

Is YAML a superset of JSON?

Yes — YAML 1.2 is a strict superset of JSON. Every valid JSON document is also valid YAML 1.2. This means you can feed JSON directly into a YAML parser and it will parse correctly. The reverse is not true: YAML documents that use comments, anchors (&), aliases (*), or implicit type coercion are not valid JSON. The superset relationship makes conversion from JSON to YAML always lossless, but YAML to JSON conversion discards any YAML-only features.

Can YAML replace JSON in REST APIs?

Technically yes — YAML is a superset of JSON, so a server could respond with YAML-formatted data. In practice, YAML should not replace JSON for REST APIs. JSON parsing is significantly faster because the spec is simpler; every browser has a native JSON.parse() function with no YAML equivalent; and JSON's whitespace-insensitivity eliminates a whole class of indentation bugs. YAML is better suited for files humans write and read. If you want a more human-readable API response format, JSON with consistent pretty-printing is a better choice than switching to YAML.

Converting between them

Because YAML 1.2 is a superset of JSON, conversion is straightforward and lossless for standard documents. The main exception is YAML-specific features like comments, anchors, and aliases, which have no JSON equivalent and are discarded during conversion.

Common YAML pitfalls to avoid

YAML’s flexibility comes with sharp edges. These are the bugs most likely to reach production undetected.

The Norway problem — unexpected boolean coercion

In YAML 1.1 (the version Kubernetes uses), bare values like yes, no, on, off, and their capitalized variants all parse as booleans. The ISO 3166-1 country code for Norway (NO) evaluates to false in a YAML 1.1 config — a real-world bug that has broken production pipelines at multiple organizations. YAML 1.2 fixed this (only true and false are booleans), but most parsers still default to 1.1 behavior.

country: NO         # parsed as false (boolean) in YAML 1.1
enabled: yes        # parsed as true (boolean)
mode: on            # parsed as true (boolean)

# Fix: always quote values that could be misinterpreted
country: "NO"
enabled: "yes"
mode: "on"

Tabs are not allowed for indentation

YAML forbids tab characters — only spaces are valid for indentation. Most editors handle this automatically, but copy-pasting YAML from shell scripts, Stack Overflow, or certain IDEs can silently introduce tabs. The resulting parse error is frustrating to debug because tabs are invisible in most editors.

Accidental number coercion

Values that look like numbers are parsed as numbers. A version string version: 1.10 becomes the float 1.1. A leading-zero value like port: 08080 may parse as octal in YAML 1.1. Quote any value that should be treated as a string: version: "1.10".

Convert between YAML and JSON

Need to switch formats? Use the free converters — paste your document and get the output instantly.

YAML to JSON JSON to YAML

Frequently Asked Questions

Why does my config file use YAML instead of JSON?

Neither is universally better. YAML is better for configuration files that humans write and maintain — it supports comments, requires less punctuation, and is easier to read. JSON is better for APIs and data exchange — it is faster to parse, has a simpler spec, and is supported natively in every environment.

Why can't I use YAML for my REST API responses instead of JSON?

YAML is a superset of JSON, so technically yes — but it should not. JSON parsing is significantly faster, the spec is simpler, and JSON is whitespace-insensitive. Using YAML for API responses would add unnecessary complexity. Use each format where it fits naturally.

Why do Kubernetes and Docker use YAML instead of JSON?

Kubernetes and Docker Compose configs are files that DevOps engineers write, read, and maintain by hand. YAML's support for comments (to explain deployment decisions), its cleaner syntax for nested objects, and its multiline string support make it far more ergonomic than JSON for this use case. Kubernetes also accepts JSON internally — YAML manifests are converted to JSON before being sent to the API server.

Is YAML faster or slower than JSON to parse?

JSON is significantly faster. JSON’s grammar is small and unambiguous; a full parser can be written in a few hundred lines of code. YAML’s specification runs to hundreds of pages and includes implicit type coercion, anchors, aliases, multiple scalar styles, and multi-document streams — all requiring more CPU work per byte. For high-throughput APIs, JSON is always the right choice. YAML’s parsing overhead is negligible for config files that are read once at startup.

What is the YAML Norway problem?

The Norway problem is a well-known YAML 1.1 quirk where the bare value NO — the ISO 3166-1 country code for Norway — is parsed as the boolean false. YAML 1.1 treats yes, no, on, off, true, false, and their capitalized variants as booleans. Kubernetes uses a YAML 1.1 parser, so this affects Kubernetes configs too. The fix is to always quote values that could be misinterpreted: country: "NO" instead of country: NO. YAML 1.2 corrected this — only true and false are booleans — but many parsers still default to 1.1 behavior.

Ready to convert yaml to json?

Open YAML to JSON Converter →
About the author

Pasindu Ishan is a software developer based in Sri Lanka. He builds developer tools at JSON Dev Tools.