DEV Community

Impeccify
Impeccify

Posted on

YAML vs JSON: What's the Difference and When to Use Each

If you've worked with config files, Docker, Kubernetes, or any CI/CD system, you've encountered YAML. If you've built APIs, you know JSON. But when should you use which?

The Syntax Difference

JSON:

{
  "name": "my-app",
  "version": "1.0.0",
  "features": {
    "auth": true,
    "cache": false
  },
  "ports": [3000, 8080]
}
Enter fullscreen mode Exit fullscreen mode

YAML (same data):

name: my-app
version: 1.0.0
features:
  auth: true
  cache: false
ports:
  - 3000
  - 8080
Enter fullscreen mode Exit fullscreen mode

YAML is less verbose and more readable. But it has strict indentation rules.

YAML Gotchas

1. Indentation matters (use spaces, not tabs)

# Wrong
parent:
    child: value  # Tab indented - BROKEN

# Correct
parent:
  child: value  # 2 spaces - WORKS
Enter fullscreen mode Exit fullscreen mode

2. Strings don't need quotes... but sometimes do

version: "1.0"     # Quotes needed - without quotes, YAML reads 1.0 as a float
enabled: "true"    # Quotes needed - without, YAML reads as boolean
port: 3000         # No quotes needed for integers
Enter fullscreen mode Exit fullscreen mode

3. Multi-line strings

description: |
  This is a multi-line
  string that preserves
  newlines.

summary: >
  This is a multi-line
  string that becomes
  one line.
Enter fullscreen mode Exit fullscreen mode

When to Use Each

Use Case Format Why
Config files YAML More readable, comments allowed
Docker Compose YAML Standard
Kubernetes YAML Standard
REST API responses JSON Universally supported
Package.json JSON Node.js standard
Database storage JSON No indentation issues
CI/CD pipelines YAML Standard (GitHub Actions, etc.)

Converting Between YAML and JSON

Use the free YAML Converter at Impeccify to convert between YAML and JSON instantly. Paste your YAML, get JSON. Paste your JSON, get YAML.

Useful when:

  • You need to convert config files
  • You're debugging an API that returns JSON but your config needs YAML
  • You're onboarding and need to understand an existing config

Top comments (0)