YAML Formatter
How it works
YAML (YAML Ain't Markup Language) is a human-readable data format widely used for configuration files, CI/CD pipelines, and infrastructure-as-code. This formatter uses js-yaml (YAML 1.2 Core schema) to parse and serialize YAML entirely in your browser — nothing is sent to a server.
Paste any YAML document and click Beautify to re-indent it consistently with your chosen spacing. Minify collapses the structure into compact single-line flow notation. → JSON converts your YAML to formatted JSON. ← JSON converts JSON input into clean YAML.
When your input contains a syntax error, the status bar shows the exact line and column and highlights that line in the gutter so you can jump straight to the problem. The YAML specification prohibits tab characters for indentation, so this formatter only offers 2-space and 4-space options.
The Tools dropdown provides two additional actions. Fix YAML 1.1 Risks scans for values that YAML 1.1 parsers (PyYAML, Docker Compose, GitHub Actions) would silently misinterpret — such as bare yes, date strings, and time-like values — and rewrites the output with those values safely quoted. Expand Anchors resolves all anchors and <<: merge keys into their final inline values, producing a self-contained document any parser can read.
After every Beautify, a built-in type coercion scanner runs automatically. It detects values that look safe in YAML 1.2 but would be silently converted to a different type (boolean, number, or date) by YAML 1.1 parsers. Warnings appear in orange in the status bar with the exact line number.
JSON ↔ YAML at a Glance
Every JSON file is valid YAML. The two formats represent the same data — just with different syntax.
JSON
{
"name": "web-app",
"replicas": 3,
"enabled": true,
"tags": ["web", "prod"],
"limits": {
"cpu": "500m",
"memory": "256Mi"
}
} YAML equivalent
name: web-app replicas: 3 enabled: true tags: - web - prod limits: cpu: 500m memory: 256Mi
| Concept | JSON | YAML |
|---|---|---|
| String | "hello world" | hello world |
| Number | 42 / 3.14 | 42 / 3.14 |
| Boolean | true / false | true / false |
| Null | null | null or ~ |
| Object | {"key": "val"} | key: val |
| Array | ["a", "b"] | - a - b |
| Comments | not supported | # comment text |
| Multi-line string | "line1\nline2" | | block scalar |
| Anchors / reuse | not supported | &anchor / *alias |
Real-world example — Kubernetes Deployment
JSON (verbose, hard to review in PRs)
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "web-app"
},
"spec": {
"replicas": 3,
"template": {
"spec": {
"containers": [{
"name": "web",
"image": "nginx:1.25",
"ports": [{"containerPort": 80}]
}]
}
}
}
} YAML (what Kubernetes actually uses)
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
template:
spec:
containers:
- name: web
image: nginx:1.25
ports:
- containerPort: 80 Paste either format into the editor above and click ← JSON or Beautify to convert instantly.
YAML Quick Reference
Core syntax rules
- Indentation uses spaces only — tabs are forbidden by the spec
- Mappings:
key: value(space after the colon is mandatory) - Sequences: leading
-(dash + space) for each item - Quote strings that contain special characters:
": # [] {} | > ! - Comments start with
#and run to end of line - A document separator
---marks the start of a new YAML document in a stream
Multi-line strings
Literal block (|) — preserves every newline
message: | Line one. Line two. Line three.
Each line break is kept exactly. Use |- to strip the trailing newline.
Folded block (>) — folds newlines into spaces
description: > This long sentence wraps across several lines but becomes one paragraph.
Single newlines become spaces. Blank lines become newlines. Use >- to strip the trailing newline.
Anchors, aliases, and merge keys
Define with &name, reuse with *name
defaults: &defaults timeout: 30 retries: 3 production: <<: *defaults host: prod.example.com staging: <<: *defaults host: staging.example.com
After Expand Anchors, every reference is inlined
defaults: timeout: 30 retries: 3 production: timeout: 30 retries: 3 host: prod.example.com staging: timeout: 30 retries: 3 host: staging.example.com
Scalar types at a glance
| Written in YAML | YAML 1.2 type | YAML 1.1 type (PyYAML, Docker) |
|---|---|---|
| true / false | bool | bool |
| yes / no / on / off / y / n | string ✓ | bool ⚠ coercion risk |
| 42 / 3.14 | int / float | int / float |
| 0755 | int 755 ✓ | int 493 (octal) ⚠ |
| 22:30 | string ✓ | int 1350 (sexagesimal) ⚠ |
| 2024-01-15 | string ✓ | Date object ⚠ |
| null / ~ | null | null |
| 'quoted' / "quoted" | string | string |
Common errors
- Mixing tabs and spaces for indentation (tabs are illegal in YAML)
- Missing space after the colon:
key:valueis a plain string, not a mapping - Unclosed flow collections:
[1, 2or{a: 1 - Unquoted strings that look like other types:
yes,on,1.0.0,v1.2 - Duplicate keys in the same mapping (silently last-write-wins in most parsers)
- Using
:or#inside an unquoted value
YAML 1.1 vs YAML 1.2 — The Hidden Trap
Why the same YAML file gives different results in Python vs JavaScript
YAML 1.2 (2009) significantly changed how scalars are typed, but many widely-used tools still ship with YAML 1.1 parsers. This means a file that looks fine in your editor can silently corrupt data when read by a different tool.
YAML 1.1 parsers (watch out)
- PyYAML — Python's default YAML library
- Docker Compose — uses go-yaml v2 (1.1 mode)
- GitHub Actions — uses go-yaml (1.1 booleans)
- Ansible — PyYAML under the hood
- Ruby's Psych — YAML 1.1 by default
YAML 1.2 parsers (modern)
- js-yaml v5+ — used by this tool
- go-yaml v3 — YAML 1.2 Core schema
- ruamel.yaml — Python, full YAML 1.2 support
- strictyaml — Python, typed subset of YAML
Real-world example — Docker Compose
services:
worker:
environment:
FEATURE_FLAG: yes # ⚠ Docker reads this as boolean true, not string "yes"
DEPLOY_TIME: 2024-06-01 # ⚠ PyYAML converts this to a datetime object
PORT: 22:30 # ⚠ go-yaml v2 reads this as integer 1350 Fix: use Tools → Fix YAML 1.1 Risks to automatically quote all risky values in one click.
Tips & Tricks
Quote anything that looks like a non-string
Version numbers (1.0), port numbers (8080 is fine, but 08080 is not), boolean-like words (yes, off), and date strings should always be quoted when you need them as strings.
Use anchors to DRY up config files
Define shared settings once with &anchor-name and reuse them with *anchor-name or merge with <<: *anchor-name. Use Expand Anchors if you need a fully resolved copy for a parser that does not support them.
Convert JSON to YAML in one click
JSON is a valid subset of YAML 1.2. Paste any JSON object into the input and click ← JSON to convert it to clean YAML in the output panel. The original JSON in the input is never modified.
Run the coercion scanner before deploying
Every time you click Beautify, the scanner runs automatically. If you see an orange ⚠ in the status bar, click Tools → Fix YAML 1.1 Risks to get a safe output before committing to your repo.
Use |- for shell scripts and SQL
When embedding multi-line shell commands or SQL in YAML (e.g. GitHub Actions run:), use the |- style to avoid a trailing newline that could break your script.
Load remote YAML files directly
Click URL in the toolbar and paste a raw GitHub URL or any public YAML endpoint. The tool fetches it and formats it immediately. If CORS blocks the request it retries via a transparent proxy.
Using YAML in Code
How to parse, format, and convert YAML programmatically in the most common environments.
Node.js — js-yaml
Install: npm install js-yaml
const yaml = require('js-yaml');
const fs = require('fs');
// Parse a YAML file into a JavaScript object
const config = yaml.load(fs.readFileSync('config.yaml', 'utf8'));
// Serialize a JavaScript object to a YAML string
const yamlStr = yaml.dump(config, { indent: 2, lineWidth: -1 });
// Convert a JSON file to YAML
const json = JSON.parse(fs.readFileSync('config.json', 'utf8'));
fs.writeFileSync('config.yaml', yaml.dump(json, { indent: 2 })); Python — PyYAML
Install: pip install pyyaml · Always use safe_load — never yaml.load() with untrusted input.
import yaml, json
# Parse a YAML file into a Python dict
with open('config.yaml') as f:
config = yaml.safe_load(f)
# Serialize a Python dict to YAML (block style)
yaml_str = yaml.dump(config, default_flow_style=False, indent=2, allow_unicode=True)
# Convert a JSON file to YAML
with open('config.json') as f:
data = json.load(f)
with open('config.yaml', 'w') as f:
yaml.dump(data, f, default_flow_style=False, indent=2) ⚠ PyYAML uses YAML 1.1 — bare yes, no, on, off are booleans. Use ruamel.yaml if you need YAML 1.2 compliance.
Python — ruamel.yaml YAML 1.2
Install: pip install ruamel.yaml · Preserves comments, anchors, and key ordering.
from ruamel.yaml import YAML
ryaml = YAML()
ryaml.preserve_quotes = True
# Parse — comments and formatting are preserved
with open('config.yaml') as f:
config = ryaml.load(f)
# Write back — comments are kept intact
with open('config.yaml', 'w') as f:
ryaml.dump(config, f) Command line — yq
Install: brew install yq or snap install yq · A portable YAML/JSON processor, like jq but for YAML.
# Pretty-print / validate a YAML file
yq '.' config.yaml
# Convert YAML → JSON
yq -oj config.yaml
# Convert JSON → YAML
yq -oy config.json
# Read a single value
yq '.metadata.name' deployment.yaml
# In-place update
yq -i '.spec.replicas = 5' deployment.yaml
# Merge two YAML files
yq '. * load("override.yaml")' base.yaml Linting — yamllint
Install: pip install yamllint · Checks style and syntax; integrates with pre-commit and CI pipelines.
# Lint with default rules yamllint config.yaml # Relaxed ruleset (fewer style warnings) yamllint -d relaxed config.yaml # Lint all YAML files in the current directory yamllint -d relaxed . # Use in GitHub Actions - name: Lint YAML run: pip install yamllint && yamllint -d relaxed .
Frequently Asked Questions
- What does YAML stand for?
- YAML stands for YAML Ain't Markup Language — a recursive acronym. It was originally called "Yet Another Markup Language" when it was created in 2001, but was later renamed to distinguish it from document markup languages like HTML and XML. YAML is a data serialization format, not a markup language.
- What is YAML used for?
- YAML is used for configuration files and data that humans need to read and write. Common use cases include: CI/CD pipeline definitions (GitHub Actions, GitLab CI, CircleCI), container orchestration (Docker Compose, Kubernetes), infrastructure-as-code (Ansible, Helm charts), application configuration (Rails, Spring Boot), and API specifications (OpenAPI/Swagger). Its clean indentation-based syntax makes it easier to read than JSON or XML for deeply nested structures.
- YAML vs JSON — what is the difference and which should I use?
- YAML and JSON both represent structured data, but serve different purposes. YAML supports comments, multi-line strings, anchors, and is much easier to read and hand-edit — making it the standard for config files. JSON has no comments, stricter syntax, and is universally supported by APIs and programming languages — making it ideal for data exchange. JSON is actually a valid subset of YAML 1.2, so any JSON file is also valid YAML. Use YAML for files humans write and maintain; use JSON for data sent between systems.
- What is a YAML formatter?
- A YAML formatter takes raw or compact YAML text and re-indents it consistently so the structure is easy to read. It also validates the YAML syntax and reports any errors with the exact line and column number.
- Is my YAML data sent to a server?
- No. All formatting, validation, and conversion happens entirely in your browser using JavaScript. Your data never leaves your machine.
- Can I convert YAML to JSON?
- Yes. Click the → JSON button to convert your YAML input to formatted JSON in the output panel. You can also click ← JSON to convert JSON input to YAML.
- Why is my YAML invalid?
- Common YAML errors include: mixing tabs and spaces for indentation, missing space after the colon in
key: value, unclosed flow collections like[1, 2or{a: 1, and unquoted strings that look like other types. When your input is invalid, the status bar shows the exact line and column number of the error and highlights that line in the gutter. - Does YAML support tab indentation?
- No. The YAML specification explicitly prohibits tab characters for indentation — they are only allowed inside quoted strings. This formatter only offers 2-space and 4-space options, which covers all valid YAML documents.
- What is the difference between YAML 1.1 and YAML 1.2?
- YAML 1.1 (used by PyYAML, Docker Compose, GitHub Actions) treats bare
yes,no,on,off,y, andnas booleans, interprets22:30as the integer 1350 (sexagesimal), and auto-converts date strings like2024-01-15into Date objects. YAML 1.2 (the current standard) only recognisestrueandfalseas booleans. This mismatch causes silent data corruption when YAML written for one parser is consumed by another. - What are YAML anchors and aliases?
- Anchors (
&name) mark a node so it can be reused, and aliases (*name) reference that node later. This avoids duplicating identical blocks across your config. The Tools → Expand Anchors button resolves all anchors and<<:merge keys into their final values, useful when your target parser does not support anchors. - How do I write multi-line strings in YAML?
- Use the literal block scalar (
|) to preserve every newline exactly, or the folded block scalar (>) to fold newlines into spaces. Append-(e.g.|-) to strip the trailing newline, which is useful for shell scripts embedded in CI config. - How do I keep a value like
1.0or0755as a string? - Wrap it in quotes. Unquoted
1.0is parsed as the float1.0(and serialised as1by most libraries). Unquoted0755is treated as octal (493) in YAML 1.1. Always writeversion: '1.0'andmode: '0755'to guarantee string type across all parsers. - Can I use this formatter for GitHub Actions or Docker Compose files?
- Yes. Paste your workflow or Compose file, click Beautify to format it, and then use Tools → Fix YAML 1.1 Risks to automatically quote any values that would be silently misinterpreted by those parsers. Both GitHub Actions and Docker Compose use YAML 1.1 parsers internally.
Related Tools
Other free developer tools available on DevFormatter.