About YAML ↔ JSON
Bidirectional YAML ↔ JSON conversion that auto-detects which direction to go from the input. Validates as it parses, so you get a precise error before any conversion. Indent control on YAML output (2 / 4 spaces). Comments are preserved through round-trips when the source format supports them.
YAML vs JSON: when to use which
YAML is human-readable; JSON is machine-parseable. Most config in 2026 starts as YAML (Kubernetes manifests, GitHub Actions, Docker Compose, Ansible) and gets converted to JSON inside tooling for processing. Round-tripping between the two is a daily move.
| YAML | JSON | |
|---|---|---|
| Readability | High — indentation, no braces, comments allowed | Lower — braces, quotes, no comments |
| Comments | Yes (#) | No |
| Multi-line strings | Yes (` | , >`) |
| Parse speed | Slower | Faster |
| Ambiguity | Higher (the NO → false trap) | Low, one canonical form |
| Best for | Files humans edit | Machine-to-machine data |
Rule of thumb: YAML for humans, JSON for machines. YAML wins on readability and usability for hand-edited config; JSON wins on speed, determinism, and native support in every language. Since YAML is a superset of JSON, any JSON document is already valid YAML — which is why conversion in this direction is lossless and the reverse only drops comments. TOML is a third choice, clearer than both for small, flat application config but weaker for deeply nested data.
Conversion details that matter
- Anchors and aliases. YAML lets you reuse blocks via
&baseand*base. JSON has no such concept — anchors get inlined, aliases get expanded into copies. The result is more verbose JSON but identical semantics. - Tags. Explicit YAML tags like
!!strare honored.!!intand!!floatensure correct typing in the JSON output. - Multiline strings.
|-style block scalars become regular JSON strings with\nescapes. Round-trip back to YAML and you get the same block style. - Booleans. We default to YAML 1.2 strict booleans (
true/falseonly). Older configs may expectyes/noto parse as boolean — toggle YAML 1.1 mode for that.
How to convert YAML to JSON in code and CLI
Test the shape here, then automate with one of these:
Python (PyYAML):
import yaml, json
json.dump(yaml.safe_load(open("in.yaml")), open("out.json", "w"), indent=2)
CLI (yq) — the fastest path for files and pipelines:
yq -o=json '.' in.yaml > out.json # YAML → JSON
yq -P '.' in.json > out.yaml # JSON → YAML
Node / npm — npm i js-yaml, then JSON.stringify(yaml.load(fs.readFileSync("in.yaml","utf8")), null, 2).
VS Code — install the YAML (Red Hat) extension; the command palette’s Convert helpers plus paste-as-JSON cover the json to yaml in vs code workflow without leaving the editor.
Converting OpenAPI and Swagger specs
OpenAPI/Swagger specs ship in either format, and toolchains disagree on which they accept. Paste your openapi.yaml here to get openapi.json for a tool that only reads JSON (or the reverse for one that wants YAML). Anchors get inlined and tags resolved, so the output validates against the same schema — no manual reflow. For very large specs, yq -o=json openapi.yaml does the same on the command line.
Common workflows
Inspect a Kubernetes manifest. Paste the YAML, see the JSON. The JSON form is what kubectl actually sends — useful for debugging.
Translate a Compose file. Docker Compose v3 is YAML; many tools want JSON. Paste, copy the JSON, feed it forward.
Convert config snippets in PRs. Copy the YAML from a code review comment, see what fields it actually populates. Faster than running the full validation pipeline.
Sanity-check generated YAML. A code generator emits YAML; round-trip it through JSON and back. The two YAMLs should be value-equivalent — if not, the generator is producing something the parser does not see.
Why our converter avoids the YAML 1.1 trap
The classic YAML footgun: country: NO parses as country: false. The Norwegian Country Code Catastrophe. Our default is YAML 1.2 — NO stays a string. If you genuinely need the old behavior (legacy Ansible, old Compose files), the toggle is one click away. Most modern YAML is 1.2-or-stricter; defaulting accordingly avoids the most common bug.
Frequently asked questions
How is direction auto-detected?
Does it preserve comments?
What YAML version is supported?
&ref / *ref), block and flow styles, multiline strings (| literal, > folded), explicit tags.Why does <code>NO</code> become <code>false</code> in my JSON?
YES, NO, ON, OFF as booleans. We default to YAML 1.2 (only true/false are booleans) — toggle YAML 1.1 mode if your input depends on the older behavior.Can it format multiline strings?
| for newline-preserving, > for folded) survive round-trips and are emitted by default for any string containing newlines longer than a threshold.How does it handle big YAML files?
yq for streaming.How do I convert YAML to JSON in Python?
import yaml, json; json.dump(yaml.safe_load(open("in.yaml")), open("out.json","w"), indent=2). Always use safe_load, not load — the latter can execute arbitrary tags. This tool does the same conversion with no install and nothing leaving your browser.When should I use YAML vs JSON?
Related tools
Last updated: 2026-07-04