跳到主要内容
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

日期和布尔值是如何转换的?

js-yaml 遵循 YAML 1.2 规范 —— 日期会被转换为 ISO 格式字符串,true/false 会被转换为布尔值。而 "yes"/"no" 则按 1.2 规范保留为字符串。

包含多个文档(以 --- 分隔)的 YAML 该怎么处理?

转换只会处理第一个文档。如需处理多个文档,请先按 `---` 将字符串拆分开。

这个工具能处理 YAML 中的 anchor 和 alias 吗?

可以 —— anchor(&)和 alias(*)会在解析阶段被解析(resolve),并在生成的 JSON 中产生对应的重复数据。