YAML and JSON are both human-readable data serialization formats used throughout modern software development. They represent the same underlying data structures but have very different syntax philosophies: JSON is strict and machine-optimized, YAML is flexible and human-optimized.
The same data in both formats
{
"app": {
"name": "Privatool",
"version": "2.0",
"debug": false,
"port": 8080
},
"features": ["privacy", "free", "fast"],
"limits": {
"max_connections": 100,
"timeout": 30,
"retry": null
}
}
The same structure in YAML:
app:
name: Privatool
version: "2.0"
debug: false
port: 8080
features:
- privacy
- free
- fast
limits:
max_connections: 100
timeout: 30
retry: null
YAML is more concise — no curly braces, no brackets, no quotes around most strings, and comments are supported with #.
Key differences at a glance
| Feature | YAML | JSON |
|---|---|---|
| Comments | Yes (#) |
No |
| Quotes for strings | Optional | Required |
| Curly braces / brackets | Optional | Required |
| Trailing commas | Not applicable | Not allowed |
| Multiline strings | Built-in (| and >) |
\n escape only |
| References | Anchors & aliases | Not supported |
| File size (same data) | Smaller | Larger |
| Parser complexity | High | Low |
| Strictness | Looser | Stricter |
| Common use | Config files | APIs, data exchange |
When to use YAML
YAML excels wherever humans write and maintain the files directly:
- Docker Compose (
docker-compose.yml): Service definitions, networking, volumes - Kubernetes manifests (
.yaml): Deployments, services, ingress, configmaps - GitHub Actions (
.github/workflows/*.yml): CI/CD pipeline definitions - Ansible playbooks: Server configuration and automation
- Application config (
config.yaml): Environment-specific settings - OpenAPI/Swagger specs: API documentation
- Hugo / Jekyll / Next.js front matter: Blog post metadata
When to use JSON
JSON is better for machine-to-machine data exchange:
- REST API responses and requests: JSON is the de facto standard
- Package manifests (
package.json,tsconfig.json,composer.json) - Browser storage (
localStorage,IndexedDB) - Language-agnostic data exchange: Every language has a built-in JSON parser
- Strict schema validation: JSON Schema is widely supported
YAML gotchas and edge cases
The Norway problem
In YAML 1.1 (used by many parsers), bare values like NO, YES, ON, OFF are interpreted as booleans. Country codes like NO (Norway) and SE (Sweden) would be incorrectly parsed. YAML 1.2 fixes this — only true and false are booleans. Always quote string values that could be misinterpreted.
# Problematic in YAML 1.1:
country: NO # Parsed as false!
# Safe in any version:
country: "NO" # Always a string
Indentation sensitivity
YAML uses spaces (never tabs) for indentation. Mixing tabs and spaces silently breaks the file. All modern text editors have a "convert tabs to spaces" setting — enable it for YAML files.
String type inference
YAML automatically infers types based on value content:
| Value | Inferred type |
|---|---|
42 |
Integer |
3.14 |
Float |
true / false |
Boolean |
null / ~ |
Null |
"42" |
String (quoted) |
'true' |
String (quoted) |
2024-01-15 |
Date (in some parsers) |
Use quotes when you need a value to remain a string regardless of its content.
Multiline strings
YAML has two multiline string styles:
# Literal block (|): preserves newlines
description: |
First line.
Second line.
Third line.
# Folded block (>): newlines become spaces
summary: >
This text will be joined
into a single line with
spaces between.
JSON has no equivalent — multiline strings require \n escape sequences.
Converting YAML ↔ JSON in practice
Configuration to API payload: Convert a YAML config file to JSON for an API request body.
API response to config: Convert a JSON API response to YAML for storage as a configuration file with added comments.
Cross-tool compatibility: Some tools accept only YAML, others only JSON. Converting between formats lets you use data across different tools.
How to convert YAML to JSON free
- Go to YAML ↔ JSON Converter
- Select direction: YAML → JSON or JSON → YAML
- Paste your content or upload a file (.yaml, .yml, or .json)
- Choose indent size (2 or 4 spaces) and optional key sorting
- Use the Swap button to convert in the opposite direction
- Copy or download the result
Parse errors are shown with the exact line number and description so you can identify and fix issues quickly.