例付きのYAML 1.2の完全な構文リファレンス。スカラー、シーケンス、マッピング、アンカー、複数行文字列、タグ、よくある落とし穴を網羅。
Plain unquoted string — most common
name: John Doe
Single-quoted: no variable or escape processing
path: '/usr/bin/node'
Double-quoted: supports escape sequences (\n, \t, etc.)
message: "Hello\nWorld"
Whole number (base 10)
port: 8080
Floating point number
threshold: 0.95
Boolean value — use only true/false (YAML 1.2)
debug: false
Null / absence of value
response: null
ISO 8601 date format
created: 2024-01-15T10:30:00Z
Single-line comment. No multi-line comment syntax.
# This is a comment key: value # inline comment
Force value to be treated as a specific type
port: !!str 8080 # string '8080' not int
Key-value pairs — indented block style
app: name: myapp port: 3000
Inline map — equivalent to block style
env: {NODE_ENV: prod, PORT: 8080}List of items — block style with dash+space
fruits: - apple - banana - cherry
Inline list — equivalent to block style
ports: [80, 443, 8080]
List where each item is a map
users:
- name: Alice
role: admin
- name: Bob
role: userArbitrarily deep nesting
database:
primary:
host: db1.example.com
port: 5432Merge the keys of a referenced mapping into current map
defaults: &defaults color: blue button: <<: *defaults text: Submit
Preserves newlines exactly as written
script: | #!/bin/bash echo "hello" exit 0
Newlines folded into spaces (great for long descriptions)
description: > This is a very long description that will be joined into one line.
Literal block, strip all trailing newlines
prompt: |- Enter value:
Literal block, keep all trailing newlines
template: |+ line1
Folded block, strip trailing newlines
message: >- This folds into one line no trailing newline
Assigns an anchor (label) to the current node
base: &base image: node:18 restart: always
References (copies) an anchored node
service1: <<: *base name: api service2: <<: *base name: worker
Anchor a scalar value to reuse it
read_timeout: &rto 30 write_timeout: *rto connect_timeout: *rto
Anchor a list
primary_colors: &colors - red - blue text_colors: *colors
Explicitly declare YAML version (optional, rarely needed)
%YAML 1.2 --- name: myapp
Marks the start of a YAML document. Required if using directives.
--- kind: Deployment --- kind: Service
Marks the end of a YAML document. Optional but useful.
--- debug: true ...
Multiple YAML docs in one file (stream)
Used in Kubernetes multi-resource files
Base64-encoded binary data
icon: !!binary | iVBORw0KGgo=
Indentation MUST use spaces only — never tabs
# WRONG: parent: \tchild: value # RIGHT: parent: child: value
Values containing ': ' (colon+space) must be quoted
# WRONG: url: http://example.com # RIGHT: url: "http://example.com"
1.0 parses as float! Quote or tag it to keep as string
version: '1.0' # string version: 1.0 # float
YAML 1.1 parses yes/no/on/off as booleans — quote to use strings
# YAML 1.1 (PyYAML): on: true # 'on' is boolean true! # Safe: always quote on: 'on'
Trailing spaces in keys are included in the key string
"key " (with space) != "key" — trim carefully
Duplicate keys — behavior is parser-specific (usually last wins). Treat as error.
Use a YAML linter to detect duplicate keys
| 機能 | YAML | JSON |
|---|---|---|
| コメント | Yes: # comment | 非対応 |
| 引用符が必要 | 文字列の場合はオプション | 文字列の場合は必須 |
| Anchors/aliases | Yes: &anchor / *alias | 非対応 |
| 複数行文字列 | Yes: | and > blocks | Escaped \n only |
| インデントのタブ | スペースのみ | どちらでも |
| JSONのスーパーセット | すべてのJSONは有効なYAML | - |
| 人間の読みやすさ | 非常に高い | 中程度 |
| パーサーの可用性 | 高い | 非常に高い |
重要: 多くのYAMLパーサーはデフォルトでYAML 1.1を使用しています(PythonのPyYAML、一部バージョンのRubyのPsych)。
YAMLはJSONのスーパーセットです — 有効なJSONはすべて有効なYAMLです。YAMLはコメント、複数行文字列、アンカー/エイリアスのサポートにより、人間が読みやすいように設計されています。JSONはより厳格で、解析が速く、普遍的にサポートされています。設定ファイルにはYAML、APIやデータ交換にはJSONを使用してください。
YAML 1.1(PyYAMLを含む多くのパーサーが使用)では、'on'、'off'、'yes'、'no'は有効なブール値です。これは通常の文字列として使用した場合(例:Ansibleで)バグを引き起こす可能性があります。YAML 1.2ではこれが修正され、'true'と'false'のみがブール値です。回避策:文字列として使う場合は常にこれらの値を引用符で囲んでください('yes'、'no')。
リテラルブロックスカラー(|)を使用して改行を保持します:ブロック内の各行が文字列内の実際の改行になります。折りたたみブロックスカラー(>)を使用して改行をスペースに変換します:長い段落に最適です。どちらもオプションのチョンピングインジケーターをサポートします(- は末尾の改行を削除、+ はすべてを保持)。
YAMLはインデントにスペースを使用します — タブは使用しません。すべての兄弟キーは同じインデントレベルにある必要があります。タブとスペースの混在は常にエラーを引き起こします。よくある修正:エディタの「タブをスペースに置換」設定を有効にする、linter(yamllint)を使用する、ファイル全体で一貫したインデント幅(2または4スペース)を確保する。