Referensi sintaks YAML 1.2 lengkap dengan contoh. Mencakup skalar, urutan, pemetaan, jangkar, string multi-baris, tag, dan jebakan umum.
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
| Fitur | YAML | JSON |
|---|---|---|
| Komentar | Yes: # comment | Tidak didukung |
| Tanda kutip diperlukan | Opsional untuk string | Diperlukan untuk string |
| Anchors/aliases | Yes: &anchor / *alias | Tidak didukung |
| String multi-baris | Yes: | and > blocks | Escaped \n only |
| Tab untuk indentasi | Hanya spasi | Keduanya |
| Merupakan superset dari JSON | Semua JSON adalah YAML yang valid | - |
| Keterbacaan manusia | Sangat tinggi | Sedang |
| Ketersediaan parser | Tinggi | Sangat tinggi |
Penting: Banyak parser YAML masih menggunakan YAML 1.1 secara default (PyYAML Python, Psych Ruby dalam beberapa versi).
YAML adalah superset dari JSON โ setiap JSON yang valid adalah YAML yang valid. YAML dirancang untuk lebih mudah dibaca manusia dengan dukungan komentar, string multi-baris, dan anchors/aliases. JSON lebih ketat, lebih cepat diurai, dan didukung secara universal. Gunakan YAML untuk file konfigurasi dan JSON untuk API dan pertukaran data.
Dalam YAML 1.1 (digunakan oleh banyak parser termasuk PyYAML), 'on', 'off', 'yes', 'no' adalah nilai boolean yang valid. Ini dapat menyebabkan bug ketika digunakan sebagai string biasa (mis., di Ansible). YAML 1.2 memperbaiki ini โ hanya 'true' dan 'false' yang merupakan boolean. Solusi: selalu beri tanda kutip pada nilai-nilai ini ('yes', 'no') jika ingin menggunakannya sebagai string.
Gunakan literal block scalar (|) untuk mempertahankan baris baru: setiap baris dalam blok menjadi baris baru yang sebenarnya dalam string. Gunakan folded block scalar (>) untuk mengubah baris baru menjadi spasi: ideal untuk paragraf panjang. Keduanya mendukung indikator chomping opsional (- untuk menghapus baris baru di akhir, + untuk mempertahankan semuanya).
YAML menggunakan spasi untuk indentasi โ tidak pernah tab. Semua kunci saudara harus berada pada level indentasi yang sama. Mencampur tab dan spasi akan selalu menyebabkan kesalahan. Perbaikan umum: aktifkan pengaturan 'ganti tab dengan spasi' di editor Anda, gunakan linter (yamllint), pastikan lebar indentasi konsisten (2 atau 4 spasi) di seluruh file.