Chuyển tới nội dung chính
Z

JSON vs YAML

JSON vs YAML compared — syntax, readability, comments, data types and speed, plus when to use each and free tools to convert between them.

JSON and YAML both describe structured data, and YAML is actually a superset of JSON — every JSON document is valid YAML. The difference is ergonomics: JSON uses explicit braces and brackets and is ubiquitous in APIs, while YAML uses indentation and is favoured for human-edited config files.

JSON vs YAML at a glance

JSON YAML
Syntax Braces {} and brackets [], quoted keys Indentation, minimal punctuation
Comments Not supported Supported with #
Readability Compact, machine-friendly Human-friendly for config
Data types Strings, numbers, bool, null, array, object Same + anchors, references, multi-line strings
Parsing Fast, everywhere Slower, indentation-sensitive
Typical use APIs, web payloads CI/CD, Kubernetes, app config

When to use JSON

Choose JSON for API responses and data interchange — it is faster to parse, supported everywhere, and unambiguous. If a machine reads it, JSON is usually the safer default.

When to use YAML

Choose YAML for files humans edit by hand — CI pipelines, Docker Compose, Kubernetes manifests — where comments and clean indentation matter more than parse speed.

Tools for JSON & YAML

JSON vs YAML

Ngày tháng và giá trị boolean được chuyển đổi như thế nào?

js-yaml sử dụng chuẩn YAML 1.2 — ngày tháng được chuyển thành chuỗi ISO, true/false thành giá trị boolean. Riêng "yes"/"no" vẫn giữ nguyên dạng chuỗi (theo đặc tả 1.2).

Còn YAML nhiều tài liệu (phân tách bằng ---) thì sao?

Chỉ tài liệu đầu tiên được chuyển đổi. Để xử lý nhiều tài liệu, hãy tách chuỗi theo `---` trước.

Công cụ có xử lý được anchor và alias trong YAML không?

Có — anchor (&) và alias (*) được giải quyết (resolve) ngay khi parse, tạo ra dữ liệu trùng lặp tương ứng trong JSON.