How to use the YAML to JSON Converter
- Paste the YAML, or open a .yaml or .yml file.
- Choose YAML 1.2 or YAML 1.1, depending on which program reads the file.
- Pick the JSON indent, whether to sort keys, and what to do with several documents.
- Fix any error it reports at the line and column shown, then copy or download the JSON.
How YAML becomes JSON
YAML and JSON describe the same kinds of data, mappings, lists, strings, numbers, booleans and null, so a YAML file converts to JSON value for value. YAML 1.2 was designed as a superset of JSON: every JSON document is already valid YAML, and the conversion back only has to drop what JSON has no syntax for, such as comments, anchors and multiple documents.
This converter parses with the yaml library, which follows the YAML 1.2 specification, then writes JSON itself so that key order and large integers come out exactly as they went in.
What happens to each YAML feature
| YAML | JSON result |
|---|---|
Comments (# ...) | Dropped; JSON has no comments |
Anchors and aliases (&base, *base) | Each alias is replaced by a copy of the anchored value |
Merge keys (<<: *base) | The anchored keys are copied in, unless the option is off |
Several documents (---) | A JSON array with one item per document, or only the first |
.inf, -.inf, .nan | null, with a note, because JSON has no infinity or NaN |
| Integers above 2^53 | Written digit for digit, with a note that JavaScript's JSON.parse will round them |
| Keys that are numbers or booleans | Strings, since JSON keys are always strings |
YAML 1.1 dates and !!binary | ISO 8601 strings and base64 strings |
An alias that refers to the node containing it (a: &x [*x]) describes data with no end, so it is reported rather than converted.
YAML 1.2 or YAML 1.1: the Norway problem
The same unquoted value means different things to different parsers. YAML 1.1, which PyYAML, Ansible, older Ruby libraries and Go's yaml.v2 (and so kubectl) follow, reads yes, no, on and off as booleans (the spec adds y and n, which PyYAML leaves as text), 0755 as an octal number and 22:22 as a base-60 number. YAML 1.2's core schema reads all of them as strings, except 0755, which becomes the decimal 755.
country: NO # 1.2: "NO" 1.1: false
mode: 0755 # 1.2: 755 1.1: 493
port: 22:22 # 1.2: "22:22" 1.1: 1342Pick the version the program that reads your file uses, and the JSON shows what that program actually sees. Quoting the value (country: "NO") makes every parser agree.
Alias bombs are refused
A "billion laughs" file defines an anchor, then a list of ten aliases to it, then a list of ten aliases to that, and so on: a few hundred bytes that expand to gigabytes. The converter caps alias expansion (the yaml library's maxAliasCount, set to 100) and says why it stopped, so a hostile file cannot freeze the page. Ordinary files with anchors stay far below that limit.
Converting on the command line
To do the same in a script, with Python or mikefarah's yq v4:
python3 -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin), indent=2, default=str))' < compose.yml
yq -o=json '.' compose.ymlPyYAML reads YAML 1.1, so it turns NO into false; yq v4 keeps it as the string NO. That difference is the most common reason two tools disagree about the same file.
Common errors
- "Tabs are not allowed as indentation": YAML indents with spaces only. Replace the tab at the line shown.
- "Map keys must be unique": the same key appears twice in one mapping. Most parsers keep the second without saying so; YAML forbids it.
- "Nested mappings are not allowed in compact mappings": an unquoted value contains a colon and a space, as in
title: Step 1: build. Quote the value. - "Implicit keys need to be on a single line": usually a quote opened on an earlier line and never closed.
Where this comes up with Shipown
Compose files are YAML, and Shipown deploys Dockerfile and Compose projects to your own server, with the ports, environment values and volumes you review before it runs. Checking a Compose file here first, or in the YAML validator, catches the indentation and quoting mistakes that otherwise surface as a failed deploy.
Frequently asked questions
Is every YAML file convertible to JSON?
Almost. Comments are dropped, anchors are expanded, and values JSON has no syntax for (.inf, .nan, dates, binary) are converted and listed. Only an alias that refers to the node containing it cannot become JSON, because the data would never end.
Why does "no" turn into false?
In YAML 1.1, unquoted yes, no, on, off, y and n are booleans. YAML 1.2 reads them as strings. Choose the version your program uses, and quote the value if it is meant as text.
How are files with several documents converted?
Documents separated by --- become a JSON array with one item per document, which suits Kubernetes files with several objects. You can also keep only the first document.
What happens to anchors, aliases and << merge keys?
Each alias is replaced by a copy of the anchored value, and merge keys copy the anchored mapping's keys into the mapping that merges them. Merge resolution can be turned off to see the << key as it is.
Is a billion-laughs YAML file safe to paste?
Yes. Alias expansion is capped at the yaml library's limit of 100, and the page reports an alias bomb instead of trying to build gigabytes of JSON.
Is my YAML uploaded anywhere?
No. Parsing and conversion run in your browser, and a file you open is read locally.