JSON ↔ YAML 转换器
在浏览器中即时转换JSON和YAML。
关于此工具
将JSON或YAML粘贴到任一面板,即可在另一面板获得即时转换结果。支持对象、数组、字符串、数字、布尔值和null。选择2或4空格缩进,一键复制结果。
使用方法
- 1 第1步:将JSON粘贴到左侧面板——YAML自动显示在右侧。
- 2 第2步:或将YAML粘贴到右侧面板——JSON显示在左侧。
- 3 第3步:使用面板上方的切换选项选择2或4空格缩进。
- 4 第4步:点击任一面板上的"复制"将结果复制到剪贴板。
JSON and YAML: the same data, two notations
JSON and YAML both describe the same underlying structures — objects (key/value maps), arrays (ordered lists), strings, numbers, booleans, and null. They differ only in how that structure is written on the page. JSON uses explicit braces, brackets, quotes, and commas, which makes it unambiguous for machines but noisy for humans. YAML uses indentation and dashes instead of brackets, which makes it cleaner to read and edit by hand. Because the data model is shared, converting between them is a matter of re-rendering the same tree in the other syntax, not changing what the data means. This tool converts JSON to YAML, and with the swap button, YAML back to JSON.
How the conversion works
Converting JSON to YAML happens in two steps. First the tool parses your JSON text into an in-memory data structure — this is also your validation step, because invalid JSON fails to parse and the tool shows the error. Second, it walks that structure and writes YAML: objects become key: value lines, arrays become - item lines, and nesting is expressed by indentation (2 or 4 spaces, your choice). Going the other direction, the tool parses the indentation-based YAML into the same kind of structure and serialises it back out as JSON.stringify with your chosen indent.
A key subtlety is string quoting. Most strings in YAML need no quotes, but some must be quoted to avoid being misread. The tool automatically quotes a string when it is empty, contains YAML-special characters (such as : # [ ] { } , & *), looks like a boolean or number (true, no, 42), or contains a newline. This prevents the classic bug where the value no is silently interpreted as the boolean false.
A worked example
Paste this JSON:
{ "name": "api", "ports": [80, 443], "tls": true }
With 2-space indentation the tool produces:
name: apiports: - 80 - 443tls: true
Notice the braces, brackets, quotes, and commas are gone. The object's keys become plain lines, the array becomes a dashed list indented under ports, and the boolean stays unquoted. Click swap and the YAML round-trips back to equivalent JSON.
The trap that makes YAML famous: indentation
In JSON, structure is defined by brackets, so whitespace is cosmetic. In YAML, indentation is the structure. Two spaces vs. three spaces, or a stray tab, changes which object a key belongs to. The two most common YAML mistakes are:
- Tabs instead of spaces. YAML forbids tabs for indentation. Always indent with spaces — this tool emits spaces and you should too.
- Inconsistent indent levels. Mixing 2-space and 4-space indentation in the same file breaks parsing. Pick one (the radio buttons here let you standardise) and keep it consistent.
Scope and limitations
This is a lightweight, dependency-free converter built for the common cases you meet in config files: nested objects, arrays, scalars, and the standard quoting rules. It does not implement every corner of the YAML 1.2 specification — advanced features such as anchors and aliases (&/*), explicit type tags, multi-document streams (--- separators), and multi-line block scalars (| and >) are outside its scope. For routine API payloads, Kubernetes-style manifests, and CI configuration it handles the job; for exotic YAML, validate with a full parser.
Use cases and privacy
- Config conversion. Turn a JSON settings file into the YAML many tools and CI systems prefer.
- Readability. Convert a dense JSON response to YAML to read or diff it more easily, then convert back.
- Learning. Paste familiar JSON and watch the YAML equivalent to learn the syntax mapping.
- Quick validation. Because conversion requires parsing, a successful convert confirms your JSON or YAML is well-formed.
Both directions run entirely in your browser. Your data — which often includes API keys, hostnames, or internal config — is never uploaded, so the tool works offline and keeps sensitive values on your own machine.