Apa itu YAML? Pengertian, Sintaks, dan Contoh Penggunaannya
YAML ("YAML Ain't Markup Language") adalah format serialisasi data yang dirancang agar mudah dibaca manusia. Tidak seperti JSON yang mengandalkan kurung kurawal dan tanda kutip, YAML menggunakan indentasi spasi untuk menunjukkan struktur data — mirip seperti Python.
Contoh Sintaks YAML
server:
host: 0.0.0.0
port: 8080
debug: true
database:
engine: postgres
replicas:
- primary
- replica-1
- replica-2
Perhatikan: tidak ada kurung kurawal, tidak ada tanda kutip wajib pada key, dan tidak ada koma di akhir baris. Struktur bersarang ditentukan murni oleh jumlah spasi indentasi (disarankan 2 spasi, jangan pakai tab).
Fitur Khas YAML yang Tidak Ada di JSON
- Komentar — bisa pakai
#untuk menjelaskan konfigurasi. - Multi-dokumen — satu file bisa berisi beberapa dokumen YAML dipisah
---. - Anchor & alias (
&dan*) — untuk reuse nilai tanpa duplikasi. - String tanpa kutip — lebih ringkas untuk dibaca, meski berisiko ambigu (lihat bagian jebakan di bawah).
Di Mana YAML Biasa Dipakai?
YAML adalah standar de-facto untuk file konfigurasi infrastruktur modern: docker-compose.yml, manifest Kubernetes, workflow GitHub Actions/GitLab CI, Ansible playbook, dan konfigurasi banyak framework backend. Karena lebih enak dibaca manusia dibanding JSON, YAML jadi favorit untuk file yang sering diedit manual oleh developer/DevOps.
Jebakan Umum YAML
- Tab vs spasi — YAML tidak menerima karakter tab untuk indentasi, harus spasi.
- Norway Problem — nilai
no,yes,on,offtanpa kutip bisa otomatis diparse jadi boolean, bukan string (terjadi pada parser YAML 1.1, termasuk kode negara "NO" untuk Norwegia yang berubah jadifalse). - Indentasi tidak konsisten — beda jumlah spasi antar level bisa membuat parser error atau salah baca struktur.
What is YAML? Meaning, Syntax, and Use Cases
YAML ("YAML Ain't Markup Language") is a data serialization format designed to be human-readable. Unlike JSON, which relies on curly braces and quotation marks, YAML uses space indentation to represent structure — similar to Python.
YAML Syntax Example
server:
host: 0.0.0.0
port: 8080
debug: true
database:
engine: postgres
replicas:
- primary
- replica-1
- replica-2
Notice: no curly braces, no mandatory quotes on keys, and no trailing commas. Nesting is determined purely by indentation depth (2 spaces is recommended, never use tabs).
YAML Features Not Found in JSON
- Comments — use
#to annotate configuration. - Multiple documents — a single file can hold several YAML documents separated by
---. - Anchors & aliases (
&and*) — reuse values without duplication. - Unquoted strings — more concise to read, though it can introduce ambiguity (see pitfalls below).
Where Is YAML Commonly Used?
YAML is the de-facto standard for modern infrastructure config files: docker-compose.yml, Kubernetes manifests, GitHub Actions/GitLab CI workflows, Ansible playbooks, and many backend framework configs. Because it's more pleasant for humans to read than JSON, it's the go-to choice for files developers and DevOps engineers edit by hand often.
Common YAML Pitfalls
- Tabs vs spaces — YAML rejects tab characters for indentation; spaces only.
- The "Norway Problem" — unquoted
no,yes,on,offget auto-parsed as booleans instead of strings (a YAML 1.1 quirk — the country code "NO" famously becomesfalse). - Inconsistent indentation — mismatched spacing between levels causes parser errors or misread structures.