A CI pipeline fails before running a single step with "bad indentation of a mapping entry". A Helm chart renders a value that is somehow a list instead of a string. A Compose file works on your machine and not on a colleague's, and the only difference is one space. YAML is pleasant to read, but because whitespace defines its structure, small mistakes cause confusing failures.
Converting YAML to JSON is one of the most effective ways to debug it. JSON has explicit brackets and quotes, so it shows you exactly what a parser understood, with no ambiguity. This guide covers how to read YAML errors, the mistakes behind most of them, what anchors and multiple documents turn into, and how to convert files safely.
What a YAML parser actually sees
YAML builds three kinds of structures: mappings (key/value pairs), sequences (lists) and scalars (single values). Indentation decides which structure a line belongs to. Consider this snippet:
items:
- a
- b
It looks like a list of two items. A parser sees a list with one item, the string "a - b", because the indented second line is treated as a continuation of the first value. Only the JSON output makes this obvious: {"items": ["a - b"]}. That is the core reason to convert: it removes the guesswork about how the file was read.
Convert YAML to JSON with our tool
The YAML to JSON Converter uses the js-yaml library in your browser, converting as you type.
- Open the YAML to JSON Converter. Paste YAML into YAML input, or click Open file to load a
.yamlor.ymlfile. Load sample shows a Docker Compose file that uses an anchor and merge keys. - Choose the JSON indent: 2 spaces, 4 spaces or Tab. Leave Expand merge keys (<<) on to resolve
<<:merges (see below). - Read the JSON output. If the YAML has an error, the pane instead shows Error on line X, column Y with the parser's message.
- Copy the JSON or Download it as a
.jsonfile.
Comments are dropped, since JSON has no place for them. Anchors and aliases are expanded into full copies, and << merge keys are resolved into ordinary keys. A file containing several documents separated by --- becomes a JSON array with one element per document; a single document becomes a plain object.
Reading and fixing the common errors
The line and column in an error message point to where the parser noticed the problem, which is often the line after the real mistake. Look at the reported line and the one above it.
"tab characters must not be used in indentation"
YAML forbids tabs for indentation. A tab can sneak in from another editor or a copied snippet and looks identical to spaces. Replace it with spaces and set your editor to insert spaces in .yml files.
"bad indentation of a mapping entry"
A key is indented at a level that does not match its siblings, or a value continues onto a line with the wrong indent:
name: web
port: 80 # error: port is indented under a scalar value
Line port up with name, or, if it was meant to be nested, give name no value and indent everything under it consistently.
"duplicated mapping key"
The same key appears twice at one level. Some tools silently keep the last one, which hides bugs; js-yaml refuses, which is more helpful. Search the file for the key and merge the two blocks.
A colon inside a value
title: Note: read first confuses the parser because : looks like the start of another mapping. Quote the value: title: "Note: read first". The same applies to values starting with *, &, !, {, [, %, @ or a backtick.
"unexpected end of the stream"
A quote, bracket or brace was opened and never closed. The error usually points to the end of the file, so search backwards for the unmatched ", [ or {.
Types: what your values become
YAML guesses the type of unquoted values. js-yaml follows the YAML 1.2 core rules, which are stricter than the older 1.1 behaviour some tools still use:
| YAML | JSON | Note |
|---|---|---|
port: 8080 |
8080 |
number |
version: 1.10 |
1.1 |
quote it to keep "1.10" |
code: 010 |
10 |
leading zero lost; quote it |
mode: 0o755 |
493 |
YAML 1.2 octal |
enabled: yes |
"yes" |
a string in 1.2; a boolean in some 1.1 tools |
empty: ~ |
null |
~ and null both mean null |
limit: .inf |
null |
JSON has no infinity |
If a value must stay text, quote it in the YAML. The conversion is the quickest way to spot values that are not the type you intended.
Anchors, aliases and merge keys
Anchors (&name) and aliases (*name) let a YAML file reuse a block:
defaults: &defaults
adapter: postgres
pool: 5
development:
database: dev
settings: *defaults
In JSON, the alias is replaced by a full copy of the anchored block, so settings becomes {"adapter": "postgres", "pool": 5}. That is correct, but it means the JSON can be much larger than the YAML if anchors are used heavily.
The << merge key copies the keys of an anchored mapping into another mapping, and any key written locally wins:
x-defaults: &defaults
restart: unless-stopped
environment:
TZ: UTC
services:
worker:
<<: *defaults
restart: on-failure
With Expand merge keys (<<) switched on, which is the default, worker becomes {"restart": "on-failure", "environment": {"TZ": "UTC"}}: the merged restart value is overridden by the local one. A list such as <<: [*base, *extra] merges several mappings, with earlier entries taking priority over later ones.
Merge keys come from YAML 1.1 and are not part of the YAML 1.2 core schema, which is why js-yaml only applies them when the merge tag is enabled, as this converter does. Docker Compose, GitLab CI and most YAML 1.1 parsers such as PyYAML also expand them, but not every tool does. Turn the switch off to see << as a literal key holding the referenced block, which is what a strict YAML 1.2 parser produces, and check the result in the tool that will actually consume the file.
Other ways to convert
- Python:
python -c "import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" < file.yaml(requires PyYAML, which follows YAML 1.1 rules, soyesbecomestrue). - yq:
yq -o json file.yamlwith the Go version of yq. - Your editor: VS Code with a YAML extension shows syntax errors as you type and can validate against schemas for Kubernetes, Compose and CI files, which catches misspelt keys that a plain parser accepts.
For CI and deployment files, schema validation in the editor is the stronger safety net. For quick "what does this actually parse to?" checks, a converter is faster.
Tips
- Re-indent a messy file with the YAML Formatter before hunting for errors; consistent indentation makes mistakes visible.
- Going the other way, the JSON to YAML Converter quotes risky strings for you.
- Once you have JSON, the JSON Formatter can minify it for an environment variable, and the JSON Schema Generator can draft a schema to validate future versions.
Frequently asked questions
What is the difference between .yml and .yaml?
None. Both extensions are used for the same format. .yaml is the officially recommended one, but many tools default to .yml.
Why are my comments missing from the JSON?
JSON has no comment syntax, so comments cannot be carried over. Keep the YAML as your source file if the comments matter.
How are multiple YAML documents converted?
A file with several documents separated by --- becomes a JSON array, with one element per document, in order.
Why did "yes" stay a string?
Under YAML 1.2 rules, which this converter uses, only true and false are booleans. Older YAML 1.1 tools also treat yes, no, on and off as booleans, so quote such values if the file is shared with those tools.
Is my file uploaded?
No. Parsing and conversion happen in your browser.