How to Convert YAML to JSON: Multi-Doc, Anchors & Types
Convert YAML to JSON the right way — handle multi-document streams, anchors and aliases, comments, and type inference. Online, in Python, and on the command line.
YAML is what humans write; JSON is what tools and APIs consume. Converting YAML to JSON is a daily task in any DevOps workflow, and while basic files convert trivially, real YAML has features — multiple documents, anchors, comments — that trip up simple parsers. This guide shows how to convert YAML to JSON correctly, online and in code.
The quickest way: convert YAML to JSON online
Copy your YAML, paste it into the YAML to JSON converter, and press Convert to JSON. The parser reads block maps, sequences, and scalars, infers types, and shows formatted JSON to copy or download.
Because config files often hold secrets, prefer a client-side tool (ours runs entirely in your browser) so nothing is uploaded. There’s no file-size cap either.
Use it when: you want a quick, private conversion on any device.
Get type inference right
Every YAML scalar is text on disk, but YAML defines types. Good conversion infers them:
42→ number,3.14→ numbertrue/false→ booleannull/~→ null- everything else → string
Quoted values stay strings — "true" becomes the string "true", not a boolean. Be aware of the classic Norway problem: in old YAML 1.1, no parsed as false, surprising everyone. Modern, predictable converters treat only true/false as booleans and leave yes/no as strings.
Handle multi-document streams
A single YAML file can hold several documents separated by ---:
---
name: service-a
---
name: service-b
Converting this should yield a JSON array of documents, not just the first one. Kubernetes manifests routinely bundle many resources this way, so multi-doc support is essential — our converter returns all documents as an array.
Anchors, aliases, and comments
Three more real-world features:
- Comments (
# like this) carry no data and should be stripped, not break the parse. - Anchors and aliases let YAML reuse a value:
&defaultsmarks it,*defaultsreferences it. A correct converter inlines the referenced value in the JSON output. - Block scalars (
|literal,>folded) and flow collections ([a, b],{k: v}) must parse correctly too.
Our converter handles all of these.
Convert YAML to JSON in Python
PyYAML is the standard:
import yaml, json
data = yaml.safe_load(open("data.yaml")) # one document
print(json.dumps(data, indent=2))
For multi-document files, use yaml.safe_load_all and wrap the result in a list:
docs = list(yaml.safe_load_all(open("data.yaml")))
print(json.dumps(docs, indent=2))
Always use safe_load — never plain load — on untrusted input.
Convert YAML to JSON on the command line
yq converts directly:
yq -p=yaml -o=json data.yaml
Or a Python one-liner:
python -c 'import sys,json,yaml; print(json.dumps(yaml.safe_load(sys.stdin)))' < data.yaml
Which method should you use?
For a quick, private conversion — especially with multi-doc files, anchors, or comments — use the online YAML to JSON converter: paste and convert, nothing uploaded. Use PyYAML inside Python pipelines and yq in the shell. Whichever you pick, mind type inference and multi-document handling — that’s where conversions go wrong.
Need the reverse? See how to convert JSON to YAML. Comparing formats? Read JSON vs XML vs YAML.
Ready to merge your JSON?
Combine files or snippets in your browser — free and private.
Open the merge toolKeep reading