YAML (YAML Ain't Markup Language) is a human-readable data serialization format primarily used for configuration files and data exchange.
Core Features of YAML
- Simplicity: Uses indentation and spaces to represent data structures without complex tags or brackets
- Readability: Designed to be easy for humans to read and write
- Cross-language Support: YAML parsers available for almost all mainstream programming languages
- Rich Data Types: Supports scalars, lists, mappings, custom types, and more
YAML vs Other Formats
Compared to JSON
- YAML is a superset of JSON, all valid JSON is valid YAML
- YAML supports comments, JSON does not
- YAML syntax is more concise, JSON syntax is stricter
- YAML supports multi-line strings, JSON does not
Compared to XML
- YAML syntax is more concise, XML requires opening and closing tags
- YAML is more readable, XML is better for machine parsing
- YAML supports richer data types
- XML has more mature validation mechanisms (Schema, DTD)
YAML Use Cases
- Configuration Files: Kubernetes, Docker Compose, CI/CD pipeline configurations
- Data Exchange: API responses, data storage
- Documentation: Technical documentation, API documentation
- Automation Scripts: Workflow definitions, task configurations
Basic YAML Syntax Rules
- Indentation: Use spaces (recommended 2 spaces), not tabs
- Key-Value Pairs: Separated by colons, must have space after colon
- Lists: Start with hyphen
- - Comments: Use
#symbol - Multi-line Strings: Use
|to preserve line breaks,>to fold line breaks
Example
yaml# This is a YAML configuration file example server: host: localhost port: 8080 features: - authentication - logging - monitoring database: type: postgresql connection: | host=db.example.com port=5432 dbname=myapp
Common Issues
- Indentation Errors: Mixing spaces and tabs causes parsing failures
- Type Conversion: YAML automatically infers data types, sometimes requiring explicit specification
- Special Characters: Certain characters need escaping or quotes
- Version Compatibility: Different YAML parsers may have subtle differences
Best Practices
- Always use space indentation, avoid tabs
- Maintain consistent indentation levels (recommended 2 spaces)
- Add comments for complex configurations
- Use YAML Schema to validate configuration files
- For sensitive data, consider using environment variables or encrypted storage