YAML to JSON Converter
How it works
YAML (YAML Ain't Markup Language) and JSON (JavaScript Object Notation) represent the same hierarchical data model — objects (mappings), arrays (sequences), strings, numbers, booleans, and null — but with very different syntax. YAML prioritizes human readability with indentation-based structure; JSON prioritizes machine-parseability with explicit delimiters.
**Syntax differences** YAML: indentation-significant, no delimiters for objects/arrays, supports comments (#), multiline strings with | (literal block) and > (folded block), multiple documents in one file (---separator). JSON: curly braces for objects, square brackets for arrays, no comments, all strings must be double-quoted, single document only.
**YAML-specific features not in JSON** Anchors and aliases: &anchor defines a reusable block; *alias references it — enabling DRY configuration. Tags: !!str, !!int force specific type interpretation. Implicit type conversion: YAML will convert "true", "yes", "on" to boolean true and "null", "~" to null unless quoted — a common source of bugs in configuration files (Norwegian problem: NO is a valid ISO country code but YAML parses it as false).
**Conversion gotchas** YAML null (~ or null) → JSON null. YAML booleans (true/false/yes/no/on/off) → JSON true/false. YAML integers and floats are preserved in JSON. YAML anchors/aliases are expanded during conversion (JSON has no anchor mechanism). YAML comments are discarded. YAML multiline strings are normalized to single-line strings in JSON.
Frequently Asked Questions
- YAML 1.1 (used by most parsers) interprets certain unquoted strings as non-string types: 'true', 'false', 'yes', 'no', 'on', 'off' → boolean. 'null', '~' → null. Country codes like 'NO' (Norway) and 'SE' (Sweden) are parsed as booleans in some YAML 1.1 parsers. Fix: always quote values that should be strings: country: 'NO'. YAML 1.2 (Go's gopkg.in/yaml.v3, newer parsers) restricts boolean parsing to only 'true'/'false', fixing the Norway problem. When generating YAML programmatically, always quote string values that could be misinterpreted.
- YAML anchor (&name) marks a block as reusable. YAML alias (*name) inserts a copy of the anchored block. Example: default: &default_settings { timeout: 30, retries: 3 }. production: <<: *default_settings, timeout: 60. When converting to JSON, anchors are expanded — the aliased values are inlined at each reference point. JSON has no anchor/alias concept. The output JSON will have the default settings copied into each environment block. YAML merge key (<<) merges an aliased map into the current map.
- Comments: YAML supports # comments; JSON has none. Multi-document files: YAML separates multiple documents with --- on a line; JSON is one document. Anchors/aliases: expanded during conversion. Multiline strings: YAML | (literal block) and > (folded block) become single-line JSON strings. Tags: !!str, !!int, !!binary are YAML-specific type annotations. Non-string keys: YAML allows integer keys ({1: 'one', 2: 'two'}); JSON requires string keys. Round-tripping YAML→JSON→YAML loses all YAML-specific features.
- Python (most reliable): python3 -c "import sys, json, yaml; json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=2)" < file.yaml. Requires PyYAML: pip install pyyaml. yq (yaml processor): yq -o=json file.yaml. The yq tool is similar to jq but for YAML. Node.js with js-yaml: node -e "const yaml=require('js-yaml'); process.stdout.write(JSON.stringify(yaml.load(require('fs').readFileSync(0,'utf8')),null,2))". Install: npm install js-yaml. All methods expand anchors and lose comments.