DEV Community

楊東霖
楊東霖

Posted on • Originally published at devplaybook.cc

JSON vs YAML Developer Guide 2025: When to Use Each Format

JSON vs YAML Developer Guide 2025: When to Use Each Format

JSON and YAML solve the same problem — structured data in text form — but they're designed for different audiences. JSON is for machines and APIs. YAML is for humans and configuration. Knowing when to reach for each saves you from unnecessary friction.

Side-by-Side Syntax

The same data in both formats:

JSON

{
  "server": {
    "host": "localhost",
    "port": 8080,
    "debug": true,
    "allowedOrigins": ["https://example.com", "https://app.example.com"]
  },
  "database": {
    "url": "postgresql://localhost:5432/mydb",
    "poolSize": 10,
    "timeout": null
  },
  "features": {
    "darkMode": true,
    "betaAccess": false
  }
}
Enter fullscreen mode Exit fullscreen mode

YAML

server:
  host: localhost
  port: 8080
  debug: true
  allowedOrigins:
    - https://example.com
    - https://app.example.com

database:
  url: postgresql://localhost:5432/mydb
  poolSize: 10
  timeout: null

features:
  darkMode: true
  betaAccess: false
Enter fullscreen mode Exit fullscreen mode

YAML is ~30% shorter and has no quotes, braces, or commas. It's designed to be read and written by humans.

Key Differences

Comments

JSON: No comments. Period. If you need to annotate a JSON file, you're out of luck without non-standard tools.

YAML: Full comment support with #:

# Database configuration
database:
  host: localhost  # Change this in production
  port: 5432
Enter fullscreen mode Exit fullscreen mode

This alone makes YAML significantly better for configuration files.

Multiline Strings

JSON: Must escape newlines:

{
  "script": "#!/bin/bash\necho \"Hello\"\necho \"World\""
}
Enter fullscreen mode Exit fullscreen mode

YAML: Two clean options:

# Literal block (preserves newlines)
script: |
  #!/bin/bash
  echo "Hello"
  echo "World"

# Folded block (newlines become spaces)
description: >
  This is a long description
  that continues across lines
  but becomes one paragraph.
Enter fullscreen mode Exit fullscreen mode

Types and Coercion

JSON is explicit — strings need quotes, booleans are true/false, null is null.

YAML is implicit — and this can surprise you:

# YAML type gotchas
port: 8080          # integer
debug: true         # boolean
name: yes           # boolean (true)! Not the string "yes"
version: 1.0        # float
country_code: NO    # boolean (false)! Not the string "NO"
date: 2025-01-01    # datetime, not string

# Force strings with quotes
name: "yes"
country: "NO"
api_version: "1.0"
date: "2025-01-01"
Enter fullscreen mode Exit fullscreen mode

JSON never has this problem — you always know what type you have.

Anchors and Aliases (YAML only)

YAML lets you reuse configuration blocks:

# Define a reusable block
defaults: &defaults
  timeout: 30
  retries: 3
  logging: true

production:
  <<: *defaults
  url: https://api.production.com
  logging: false  # Override

staging:
  <<: *defaults
  url: https://api.staging.com
Enter fullscreen mode Exit fullscreen mode

There's no equivalent in JSON — you'd copy-paste.

Performance

JSON wins significantly on parsing speed. Benchmarks across implementations:

Format Relative Parse Speed
JSON 1× (baseline)
YAML 5–10× slower

For configuration files loaded once at startup, this doesn't matter. For high-frequency parsing in APIs or data pipelines, JSON is the clear choice.

JSON parsers are also simpler and safer — YAML parsers have historically had security issues (arbitrary code execution in older parsers via !!python/object in PyYAML, for example).

Tooling

JSON

Native in every language:

// JavaScript
JSON.parse('{"key": "value"}');
JSON.stringify({ key: 'value' }, null, 2);
Enter fullscreen mode Exit fullscreen mode
# Python
import json
data = json.loads('{"key": "value"}')
json.dumps(data, indent=2)
Enter fullscreen mode Exit fullscreen mode
# Command line
cat data.json | jq '.server.host'
Enter fullscreen mode Exit fullscreen mode

jq is the gold standard for JSON processing in the terminal. Nothing comparable exists for YAML.

YAML

# Python
import yaml
with open('config.yaml') as f:
    config = yaml.safe_load(f)  # Always use safe_load, not load()
Enter fullscreen mode Exit fullscreen mode
// Node.js
const yaml = require('js-yaml');
const config = yaml.load(fs.readFileSync('config.yaml', 'utf8'));
Enter fullscreen mode Exit fullscreen mode
# Command line — yq (like jq but for YAML)
cat config.yaml | yq '.server.host'
Enter fullscreen mode Exit fullscreen mode

When to Use JSON

1. APIs and HTTP responses

JSON is the standard for REST APIs. Every HTTP client handles it, and the Content-Type application/json is universally understood.

POST /api/users HTTP/1.1
Content-Type: application/json

{"name": "Alice", "email": "alice@example.com"}
Enter fullscreen mode Exit fullscreen mode

2. Data exchange between services

When two systems communicate, JSON is the safest format — it's simpler, faster to parse, and has no type coercion surprises.

3. Schema definitions

JSON Schema is the standard for documenting API request/response shapes. OpenAPI specs use JSON (or YAML, but JSON is the original).

4. Package files

package.json, tsconfig.json, composer.json — ecosystem tooling expects JSON.

5. Browser/JavaScript environments

Native JSON support means zero parsing overhead.

When to Use YAML

1. Configuration files

Docker Compose, Kubernetes manifests, GitHub Actions, Ansible playbooks, Helm charts — the entire DevOps ecosystem standardized on YAML.

# docker-compose.yml
services:
  web:
    image: node:20
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://db:5432/app
    depends_on:
      - db

  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data
Enter fullscreen mode Exit fullscreen mode

2. CI/CD pipeline definitions

# .github/workflows/ci.yml
name: CI
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm test
Enter fullscreen mode Exit fullscreen mode

3. Human-maintained files

If a developer edits the file directly (not programmatically), YAML's comment support and cleaner syntax reduce mistakes.

4. Multi-document files

YAML allows multiple documents in one file using ---:

---
name: Service A
version: 1.0
---
name: Service B
version: 2.1
Enter fullscreen mode Exit fullscreen mode

Converting Between Formats

Use DevPlaybook's YAML to JSON converter for quick conversions.

Or via CLI:

# YAML to JSON
cat config.yaml | python3 -c "import sys,yaml,json; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))"

# JSON to YAML
cat data.json | python3 -c "import sys,yaml,json; print(yaml.dump(json.load(sys.stdin), default_flow_style=False))"
Enter fullscreen mode Exit fullscreen mode
// Node.js
const yaml = require('js-yaml');
const fs = require('fs');

// YAML → JSON
const yamlContent = fs.readFileSync('config.yaml', 'utf8');
const obj = yaml.load(yamlContent);
fs.writeFileSync('config.json', JSON.stringify(obj, null, 2));

// JSON → YAML
const jsonContent = JSON.parse(fs.readFileSync('config.json', 'utf8'));
fs.writeFileSync('config.yaml', yaml.dump(jsonContent));
Enter fullscreen mode Exit fullscreen mode

Common Mistakes

YAML tab indentation — YAML forbids tabs. Use spaces only:

# Wrong
server:
    host: localhost  # TAB — will fail

# Right
server:
  host: localhost  # 2 spaces
Enter fullscreen mode Exit fullscreen mode

JSON trailing comma — JSON is strict; trailing commas crash parsers:

// Wrong
{
  "name": "Alice",
  "age": 30,   trailing comma
}

// Right
{
  "name": "Alice",
  "age": 30
}
Enter fullscreen mode Exit fullscreen mode

YAML implicit booleans — Quote values that look like booleans if you want strings:

# Dangerous
enabled: yes    # boolean true
country: NO     # boolean false
on: true        # boolean true

# Safe
enabled: "yes"
country: "NO"
Enter fullscreen mode Exit fullscreen mode

Decision Framework

Is this an API response or payload?
   JSON

Is this a configuration file humans will edit?
   YAML

Is this inside a Kubernetes/Docker/GitHub Actions context?
   YAML (ecosystem standard)

Do you need comments?
   YAML

Is parsing speed critical (>1000 parses/sec)?
   JSON

Is this a schema or spec file?
   JSON Schema (JSON) or OpenAPI (either)
Enter fullscreen mode Exit fullscreen mode

Both formats are here to stay. JSON owns the API and data exchange world. YAML owns the configuration and DevOps world. Use the right tool for the context.


Level Up Your Dev Workflow

Found this useful? Explore DevPlaybook — cheat sheets, tool comparisons, and hands-on guides for modern developers.

🛒 Get the DevToolkit Starter Kit on Gumroad — 40+ browser-based dev tools, source code + deployment guide included.

Top comments (0)