YAML to JSON Converter (bidirectional)
Convert YAML to JSON and JSON to YAML in one place. Auto-detects the direction, expands anchors and aliases, handles multi-document streams, pinpoints parse errors by line, and verifies every conversion with a round-trip check. Runs fully in your browser — nothing is uploaded.
How it works
The converter is a deterministic parse-then-serialize pipeline. There are no heuristics and no network calls: the YAML 1.2.2 Core Schema fixes how every scalar resolves, and RFC 8259 fixes the JSON it emits, so the same input always produces the same output.
- Detect direction. In Auto mode the tool checks the first non-whitespace character. A leading
{or[that also parses as JSON means you pasted JSON and want YAML. Everything else is treated as YAML. Because JSON is a strict subset of YAML 1.2, a YAML parser accepts both — the first-character check disambiguates your intent. You can override it with the toggle. - YAML → JSON. The input is parsed with
loadAllso multi-document streams (separated by---) are captured. Scalars resolve per the Core Schema:null/~/empty become JSON null,true/falsebecome booleans, decimal/hex/octal integers and floats become numbers, and anything else is a string. Anchors and aliases are expanded and merge keys (<<) are resolved, because JSON has no reference mechanism. The structure is serialized withJSON.stringifyat your chosen indent. - JSON → YAML. The input is parsed with
JSON.parseand re-emitted as YAML in block style (or inline flow style if you prefer), with key order preserved unless you turn on sorting. - Validate. A parse failure never blanks the screen. The exception is caught and the first error is shown with its 1-based line and column plus a short snippet, so you can jump straight to it.
- Round-trip check.The output is re-parsed and deep-compared against the original structure. Identical means "round-trip verified". If a construct cannot survive — for example
.infor.nan, which JSON renders as null — the result is marked lossy and the affected path is named.
This same engine (js-yaml on the YAML 1.2 schema) powers the worked examples below, which reproduce exactly in the live tool.
YAML ↔ JSON type mapping
Every conversion rule the tool applies, drawn from the YAML 1.2.2 Core Schema and RFC 8259. The * on the infinity/NaN row marks a construct JSON cannot represent.
| YAML | JSON | Notes |
|---|---|---|
| null, ~, (empty) | null | All YAML null forms collapse to JSON null. |
| true / false | true / false | Boolean. Only the canonical lowercase forms in the Core Schema. |
| 42, 0x2A, 0o52 | 42 | Integers: decimal, hex (0x) and octal (0o) all resolve to a number. |
| 3.14, 1e3 | 3.14, 1000 | Floats keep their numeric value. |
| .inf, -.inf, .nan | null * | JSON has no Infinity/NaN — these become null and are flagged as lossy. |
| "text", text | "text" | Any non-resolving scalar is a string; JSON always quotes it. |
| - a\n- b (block) / [a, b] (flow) | [a, b] | Both block and flow sequences become a JSON array. |
| key: val / {key: val} | {"key": val} | Block and flow mappings become a JSON object. |
| &anchor / *alias | (expanded) | JSON has no references — aliases are materialised into duplicated subtrees. |
| <<: *anchor (merge) | (merged keys) | Merge keys are resolved into the surrounding object. |
| doc1\n---\ndoc2 | [doc1, doc2] | A multi-document stream becomes a JSON array of documents. |
| # comment | (dropped) | JSON has no comments; they are not preserved. |
Worked examples
YAML → JSON, with an anchor, merge key and null
defaults: &d retries: 3 timeout: 30 prod: <<: *d timeout: 60 enabled: true region: ~
{
"defaults": { "retries": 3, "timeout": 30 },
"prod": {
"retries": 3,
"timeout": 60,
"enabled": true,
"region": null
}
}- &d defines the anchor { retries: 3, timeout: 30 }.
- prod merges it with <<: *d, then overrides timeout to 60.
- enabled: true stays a boolean; region: ~ resolves to null.
- The anchor is expanded (JSON has no references).
JSON → YAML, nested object and array
{ "name": "induwara",
"tags": ["seo", "tools"],
"meta": { "live": true, "rank": null } }name: induwara tags: - seo - tools meta: live: true rank: null
- The object becomes a block mapping.
- The array becomes a block sequence with - markers.
- true stays boolean; null is emitted as null.
- Round-trip: re-parsing the YAML returns the original object.
Lossy edge case — .inf in YAML
bandwidth: .inf samples: 1000
{
"bandwidth": null,
"samples": 1000
}- .inf resolves to Infinity under the YAML Core Schema.
- JSON (RFC 8259) cannot represent infinity, so it becomes null.
- The tool still converts but marks the result lossy and names the path ($.bandwidth).
- samples: 1000 converts cleanly as an integer.
Frequently asked questions
Sources & references
- YAML 1.2.2 Specification — scalar resolution, anchors/aliases, merge keys, multi-document streams
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format
- ECMA-404 — The JSON Data Interchange Syntax
Conversion rules were last cross-checked against the YAML 1.2.2 spec and RFC 8259 on 2026-06-25. The tool uses the js-yaml parser on the YAML 1.2 Core/Default schema.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.