Skip to content
TypeParser
All tools

YAML ↔ JSON

Convert between YAML and JSON.

beats onlineyamltools.com edge: Auto-detect direction + indent control
input
output
direction indent
paste to convert
Guide

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.

YAMLJSON
ReadabilityHigh — indentation, no braces, comments allowedLower — braces, quotes, no comments
CommentsYes (#)No
Multi-line stringsYes (`, >`)
Parse speedSlowerFaster
AmbiguityHigher (the NOfalse trap)Low, one canonical form
Best forFiles humans editMachine-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 &base and *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 !!str are honored. !!int and !!float ensure correct typing in the JSON output.
  • Multiline strings. |-style block scalars become regular JSON strings with \n escapes. Round-trip back to YAML and you get the same block style.
  • Booleans. We default to YAML 1.2 strict booleans (true/false only). Older configs may expect yes/no to 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 / npmnpm 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?
We try parsing as YAML first (it is a superset of JSON for our purposes). If the input is valid JSON, we convert to YAML; otherwise we treat it as YAML and convert to JSON. You can pin direction with the toggle.
Does it preserve comments?
YAML supports comments; JSON does not. YAML → JSON drops comments. JSON → YAML inserts them only if you provide a hint (we do not invent commentary). For round-trip preservation, edit YAML directly.
What YAML version is supported?
YAML 1.2 (the modern spec). Includes anchors and aliases (&ref / *ref), block and flow styles, multiline strings (| literal, > folded), explicit tags.
Why does <code>NO</code> become <code>false</code> in my JSON?
YAML 1.1 (the older spec, still common) treats 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?
Yes. Block scalars (| 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?
Parsing runs on a Web Worker. Files in the 1-10 MB range convert instantly. Above that, prefer command-line yq for streaming.
How do I convert YAML to JSON in Python?
Use PyYAML plus the stdlib JSON module: 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?
Use YAML for files humans edit — config, CI pipelines, Kubernetes manifests — because it allows comments, multi-line strings, and anchors, and is less noisy. Use JSON for machine-to-machine data — APIs, logs, storage — because it parses faster, has one canonical form, and every language reads it natively. YAML is a superset of JSON, so any JSON is already valid YAML. (TOML is a third option, better than both for small, flat app config.)

Related tools

Last updated: 2026-07-04