Indentation
- Uses spaces for nesting, no tabs.
- Consistent indentation is crucial.
A concise guide to the YAML data serialization format, covering syntax, data types, collections, and advanced features for configuration files and data exchange.
Indentation
|
Comments
|
Key-Value Pairs
|
Lists (Sequences)
|
Maps (Mappings)
|
Case Sensitivity
|
Document Separators
|
Root Element
|
Reserved Characters
|
Whitespace
|
Example Structure:
|
Example List:
|
Plain Scalars (Most Common)
|
Quoted Scalars
|
Numeric Types
|
Boolean Types
|
Null Type
|
String Examples:
|
Multi-line Strings
|
Literal Block Example:
|
Folded Block Example:
|
Explicit Typing (Tags)
|
Date & Time Types
|
Binary Data
|
Block Style List
|
Example Block List:
|
Nested Block Lists
|
Example Nested Lists:
|
Flow Style List
|
Example Flow List:
|
List Containing Maps
|
Example List of Maps:
|
Empty List
|
Example Empty List:
|
Using
|
Combined List/Map Structure:
|
Block Style Map
|
Example Block Map:
|
Nested Block Maps
|
Example Nested Maps:
|
Flow Style Map
|
Example Flow Map:
|
Maps Containing Lists
|
Example Map with List:
|
Complex Keys (
|
Empty Map
|
Example Empty Map:
|
Combining Styles
|
Anchors (
|
Aliases (
|
Usage:
|
Example Basic Usage:
|
Merging (
|
Anchors for Lists/Scalars:
|
Best Practice:
|
Caution:
|
Purpose:
|
Syntax:
|
Standard YAML Tags:
|
Example Standard Tag:
|
Custom Tags:
|
Example Custom Tag:
|
Local Tags:
|
Global Tags:
|
Directives (less common for users):
|
Tagging Anchored Nodes:
|
Purpose:
|
Separator (
|
End Marker (
|
Example Multiple Documents:
|
Reading Multiple Documents
|
Common Use Cases:
|
Each document is independent
|
Directives
|
Use Spaces, Not Tabs
|
Consistent Indentation
|
Quote Strings Wisely
|
Keep Lines Readable
|
Comments are Your Friend
|
Avoid Trailing Whitespace
|
Use Anchors/Aliases for Repetition
|
Prefer Block Style for Structure
|
Use Flow Style for Simple Collections
|
Validate Your YAML
|
Using Tabs for Indentation: Leads to parsing errors. Always use spaces. |
Inconsistent Indentation: Mixing space counts (e.g., 2 spaces here, 4 spaces there) breaks structure. |
Forgetting Space After Colon/Dash: |
Unquoted Strings: Values like |
Special Characters: Forgetting to quote or escape strings containing |
Complex Keys: Using non-string keys in maps without the |
Unexpected Type Coercion: YAML’s flexibility in type inference can sometimes lead to values being interpreted differently than intended (e.g., |