YAML Syntax Guide
What is YAML?
YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It's widely used for configuration files in Docker, Kubernetes, CI/CD pipelines, and application settings.
Basic Syntax
# Key-value pairs name: Alice age: 30 # Nested objects (use 2-space indent) address: city: London country: UK # Lists hobbies: - reading - cycling - cooking # Inline list colors: [red, green, blue]
Data Types
string: "hello" # Quoted string unquoted: hello # Also a string number: 42 # Integer float: 3.14 # Float boolean: true # Boolean (true/false, yes/no) null_value: null # Null (null or ~) date: 2024-01-15 # Date multiline: | # Block literal (preserves newlines) Line one Line two
Anchors & Aliases
# Define anchor with &, reference with * defaults: &defaults timeout: 30 retries: 3 production: <<: *defaults # Merge anchor timeout: 60 # Override specific values
Common Mistakes
- Tabs instead of spaces: YAML requires spaces for indentation, never tabs.
- Unquoted special values:
yes,no,on,offare interpreted as booleans. Quote them if you mean strings. - Inconsistent indentation: pick 2 spaces and stick to it throughout the file.
- Colon in values:
key: value: herebreaks. Quote the value:key: "value: here".
Convert and validate YAML with our YAML to JSON and YAML Validator tools.
FAQ
YAML vs JSON, when to use which?
YAML is better for human-edited config files (comments, readability). JSON is better for machine-to-machine data exchange (strict, widely supported).
Can YAML have comments?
Yes! Use # for comments. This is a major advantage over JSON which has no comment syntax.