JSON vs YAML: Key Differences and When to Use Each

JSON Formatter Hub

JSON vs YAML: Key Differences and When to Use Each

JSON and YAML are both text-based formats for representing structured data, and both appear constantly in modern software development. JSON dominates REST APIs and browser applications; YAML dominates CI/CD pipelines, Kubernetes, and configuration files. Understanding the trade-offs helps you pick the right format and avoid the surprising pitfalls each one carries.

Side-by-Side Syntax Comparison

The same application configuration written in both formats:

JSON:

{
  "app": "my-service",
  "version": "2.1.0",
  "server": {
    "host": "0.0.0.0",
    "port": 8080,
    "debug": false
  },
  "database": {
    "host": "db.example.com",
    "ssl": true
  },
  "allowedOrigins": [
    "https://app.example.com",
    "https://www.example.com"
  ],
  "description": "Main API service"
}

YAML:

# Application configuration
app: my-service
version: "2.1.0"
server:
  host: "0.0.0.0"
  port: 8080
  debug: false
database:
  host: db.example.com
  ssl: true
allowedOrigins:
  - https://app.example.com
  - https://www.example.com
description: Main API service

YAML is visibly less noisy โ€” no braces, fewer quotes, and comments are allowed. JSON is more explicit about every boundary.

YAML Advantages

Comments

YAML supports # comments. JSON does not. This is the most practically important difference for config files that humans edit:

# Database settings โ€” update host before deploying to production
database:
  host: db.prod.example.com  # change this for staging
  port: 5432

Multi-line strings

YAML has two block scalar styles. The literal block (|) preserves newlines; the folded block (>) folds them into spaces:

message: |
  Dear user,
  Your account has been created.
  Welcome aboard!

summary: >
  This text spans multiple
  lines but will be joined
  into a single line.

Anchors and aliases (DRY config)

YAML lets you define a block once and reuse it with & and *:

defaults: &defaults
  retries: 3
  timeout: 30

production:
  <<: *defaults
  host: prod.example.com

staging:
  <<: *defaults
  host: staging.example.com

JSON Advantages

Strict syntax โ€” fewer surprises

JSON has one way to write every value. YAML has many, and they interact unexpectedly. JSON parse errors are rare after learning the four rules (double quotes, no trailing commas, no comments, no unquoted keys). YAML parse errors are often invisible โ€” the parser accepts the file but silently produces the wrong value.

Universal browser and API support

Every browser supports JSON.parse() natively. Every REST framework defaults to JSON. YAML requires a library in virtually every language. For HTTP APIs, configuration-as-code that runs in the browser, and database documents, JSON is the practical default.

Faster to parse

JSON parsers are highly optimized because the format is so simple. YAML parsers must handle indentation sensitivity, implicit type coercion, multiple quoting styles, anchors, and more โ€” making them slower and harder to implement correctly.

YAML Gotchas You Must Know

The Norway problem (implicit boolean coercion)

In YAML 1.1 (used by many tools including older Kubernetes versions), unquoted country codes like NO, yes, on, off are parsed as booleans. NO becomes false. This breaks locale codes, toggle names, and short keys silently:

# Dangerous in YAML 1.1
countries:
  - NO   # parsed as false, not the string "NO"
  - GB
  - US

# Safe: always quote ambiguous values
countries:
  - "NO"
  - GB
  - US

Indentation is structural

A single wrong indent level moves a key to the wrong parent object. JSON uses braces, so indentation is cosmetic. In YAML it is syntax:

# WRONG โ€” logging is a sibling of server, not a child
server:
  host: localhost
logging:     # intended to be under server
  level: debug

# CORRECT
server:
  host: localhost
  logging:
    level: debug

Strings that look like other types

Unquoted values are type-coerced. Version numbers, port numbers written as strings, and other values can be misinterpreted:

version: 1.10    # parsed as float 1.1, not string "1.10"
version: "1.10"  # correct โ€” string preserved

port: 8080       # fine โ€” integer
country: NO      # problem โ€” boolean false in YAML 1.1

Where Each Format Wins

Use CaseBest ChoiceWhy
REST API responseJSONNative browser support, universal client expectation
Kubernetes manifestsYAMLLess verbose, comments, official format for K8s
GitHub Actions workflowYAMLMultiline scripts, comments, official format
Docker ComposeYAMLAnchors for shared config, readability
package.json / tsconfig.jsonJSONNode.js tooling expects JSON
Database documentsJSONMongoDB, Firestore, DynamoDB use JSON natively
Human-edited app configYAMLComments explain intent; less punctuation to misplace
Machine-generated configJSONEasier to generate correctly; no indentation to manage

YAML is a JSON Superset (With Caveats)

The YAML 1.2 specification states that JSON is valid YAML. In practice, most YAML parsers accept valid JSON. This means you can gradually migrate a JSON config to YAML by renaming it and adding comments without changing any values. However, do not rely on this in production without testing โ€” older YAML 1.1 parsers have edge cases where valid JSON is rejected.

Quick Decision Guide

Related Articles