YAML is a human-readable serialization format commonly used for configuration. Its indentation and scalar rules make it concise, but small formatting mistakes can change the resulting data.
Mappings and lists
yaml_snippet
YAML · 9 lines|
| name: example |
| retries: 3 |
| enabled: true |
| tags: |
| - dev |
| - operations |
| server: |
| host: localhost |
| port: 8080 |
Use spaces for indentation, not tabs. Members at the same nesting level must align. A list item begins with
- , and a mapping generally uses key: value.Strings and comments
yaml_snippet
YAML · 3 lines|
| message: "Hello\nworld" |
| literal: 'Backslashes are literal here' |
| # A comment |
Quote strings when their content might otherwise be interpreted as another type or syntax. Double-quoted strings support escapes; single-quoted strings use doubled single quotes to represent a quote. Parser versions and schemas may interpret some unquoted scalars differently.
Multiline text
yaml_snippet
YAML · 6 lines|
| literal: | |
| First line |
| Second line |
| folded: > |
| This usually becomes |
| one line. |
| preserves line breaks; > folds many line breaks into spaces. Chomping indicators such as |- control trailing newlines.Documents, anchors, and tags
--- starts a document and ... can mark its end. Anchors (&name) and aliases (*name) allow reuse within a document. Explicit tags such as !!str can request a type, subject to parser support.Validate YAML with the parser and schema used by the target application, and use safe loading when parsing untrusted input.
✦ 卷终 · 柯航存卷 ✦