TOOL / 02
YAML 格式化
粘贴 YAML,即可格式化、校验或转换为 JSON。内容仅在当前浏览器中处理。
格式化
统一缩进与结构,让内容更易读。
校验
有语法问题时,会提示具体的位置。
本地处理
零上传、零存储,内容始终留在本地。
一、YAML 适合写什么
YAML 的设计目标只有一个:让人读得下去。它用缩进表达层级、用短横线表达列表,几乎没有括号和引号,所以特别适合人来手写的场景——Kubernetes 清单、CI 流水线定义、Ansible 剧本、Docker Compose、静态站点配置,基本都是 YAML。
代价是:它对格式极其敏感。少一个空格、多一个 Tab,含义就变了,甚至直接报错。
二、三条必须记住的规则
- 只能用空格缩进,不能用 Tab。YAML 规范明确禁止制表符。从编辑器里复制过来的内容如果混了 Tab,报错往往指向一个看起来完全正常的行——这是最难查的一类问题。
- 同一层级缩进必须完全一致。有的人写 2 个空格,有的人写 4 个,混在一起就会把同级的两个键变成父子关系。
- 冒号后面要有一个空格。
name:JiangYu不是一个键值对,而是一个字符串"name:JiangYu"。
三、怎么用
- 粘贴 YAML,点「格式化」,工具会统一缩进并检查语法;
- 如果报错,按提示的行号去看那个位置——通常是缩进异常或缺少冒号后的空格;
- 需要给程序消费时,点「转为 JSON」,得到带引号的标准结构;
- 把结果复制回编辑器。
四、类型推断带来的意外
YAML 会自动猜类型,这在配置里是个双刃剑:
| 你写的 | 可能被读成 | 怎么避免 |
|---|---|---|
1.20 | 数字 1.2 | 加引号 "1.20" |
0755 | 八进制 493 | 加引号 "0755" |
on / no | 布尔值(部分解析器) | 加引号 |
2026-10-24 | 日期对象 | 加引号 |
这也是"配置文件在本地没问题,部署到服务器就报错"的常见原因:两边用的解析器版本不同,类型推断规则就不完全一样。凡是"看起来像数字/日期但其实要当字符串"的值,加引号最稳。
五、数据安全
解析完全在浏览器内完成,不会上传内容。但配置文件里经常含密码和令牌,用完建议清空输入框。
What YAML is good for
YAML has exactly one design goal: stay readable. Indentation carries the hierarchy, dashes mark lists, and there are almost no brackets or quotes, which makes it ideal for anything a human hand-edits: Kubernetes manifests, CI pipeline definitions, Ansible playbooks, Docker Compose, static site config.
The trade-off is fragility. One missing space or one stray tab changes the meaning, or fails outright.
Three rules worth memorising
- Spaces only, never tabs. The spec forbids tab characters. A tab pasted in from an editor produces an error on a line that looks perfectly fine — the hardest kind to find.
- Indentation must match exactly at the same level. Mixing two and four spaces silently turns sibling keys into a parent-child relationship.
- A colon needs a space after it.
name:JiangYuis not a key/value pair; it is the string"name:JiangYu".
How to use it
- Paste the YAML and press Format to normalise indentation and check the syntax;
- If it errors, jump to the reported line — it is almost always indentation or a missing space after a colon;
- Use Convert to JSON when a program will consume it;
- Copy the result back into your editor.
When type inference bites
YAML guesses types automatically, which cuts both ways in configuration files:
| You write | May be read as | How to avoid it |
|---|---|---|
1.20 | the number 1.2 | quote it: "1.20" |
0755 | octal 493 | quote it: "0755" |
on / no | a boolean (in some parsers) | quote it |
2026-10-24 | a date object | quote it |
This is also why a config that works locally can fail in production: the two sides run different parser versions with slightly different inference rules. If a value looks like a number or a date but must stay a string, quoting it is always the safe move.
Data safety
Parsing happens entirely in your browser. Config files often carry passwords and tokens, so clear the input when you are done.