How to Compare Two YAML Files Without Getting Bitten by Indentation

To compare two YAML files, bring them to the same indentation and key order, then put them side by side in a diff tool. That first part matters more with YAML than with almost any other format. A file re-indented from two spaces to four, or saved by an editor that alphabetizes keys, loads to exactly the same data and still shows up as a wall of red and green.

Below: why that happens, what each comparison method is good for, and a real Docker Compose change walked through from a noisy diff to the four lines that matter. The comparing itself happens on our YAML compare page, in your browser.

Why YAML files are deceptively hard to compare

YAML is a whitespace-sensitive format (see the YAML 1.2.2 specification), and that is exactly what makes diffs tricky. Indentation carries meaning, but the amount of indentation does not, as long as it is consistent. So one file indented with two spaces and another with four can load to the identical structure while every line looks different to a text diff.

Key order is the other half. A YAML mapping is a set of key/value pairs, and their order carries no meaning. These two container snippets are the same mapping:

image: nginx:1.27
imagePullPolicy: IfNotPresent
imagePullPolicy: IfNotPresent
image: nginx:1.27

A line diff still marks both lines as changed. Sequences are the exception: the items in a list keep their order, so moving a list item up or down is a real change.

Sorting YAML noise from real changes
What you see in the diffIs it a real change?What to do
2-space vs 4-space indentNo, only consistency mattersRe-emit both files with yq
Mapping keys in a different orderNo, mappings are unorderedSort keys on both sides with yq
true vs "true"Yes, boolean vs stringInvestigate, this is real
image: nginx vs image: "nginx"No, same string valueIgnore, or normalize quoting
Flow style [a, b] vs block listNo, same sequencePick one style for both
Sequence items in a different orderYes, sequences are orderedInvestigate, this is real

Quotes are where people get caught. true without quotes is a boolean and "true" is a string, and mixing those up has broken plenty of deployments. Quoting a plain word like nginx changes nothing, though. The spec's section on the core schema lists exactly which unquoted values turn into booleans, numbers and nulls.

Ways to compare YAML

Which one you pick comes down to whether the files are on disk, in git, or in a chat message someone just sent you.

MethodGood forHandles key order?
Online side-by-side diffFragments pasted from a PR, a ticket or a chatNo, it compares the text as written
yq then diffFiles on disk, CI scriptsYes, once you sort the keys
git diffChanges already committedNo, line-based
dyffLarge Kubernetes manifestsYes, it compares the parsed documents

In practice the two mix well: let yq rewrite both files into one canonical layout, then read the result side by side, where a changed value sits right next to the old one instead of being split across a - and a + line.

Comparing two YAML files, step by step

This is what I run when two copies of a manifest disagree and nobody remembers who touched what.

  1. If the files came from different people or editors, normalize both: yq -P 'sort_keys(..)' old.yaml > old.norm.yaml, and the same for the new file. That re-indents everything the same way and puts every mapping's keys in alphabetical order.
  2. Open the YAML compare tool.
  3. Put the old file in the left pane and the new one in the right.
  4. Click Format on each side. It strips trailing spaces and extra blank lines at the end, which otherwise show up as changes you cannot see.
  5. Check the badge above each pane. A tab used for indentation is flagged with its line number, and it is the most common reason a YAML file refuses to load at all.

Step one does the heavy lifting. Our YAML tool compares the text exactly as you paste it and does not reorder keys for you, so a file sorted by yq and one that was not will still disagree on every line. The comparison itself uses Google's diff-match-patch and pairs each edited line with its original, so a changed value highlights only the characters that differ.

A worked example: a Docker Compose service

Here is a service definition from a Compose file, as it sits on the main branch:

services:
  api:
    image: shop/api:1.8.2
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      LOG_LEVEL: info
      CACHE_TTL: "300"

A teammate's branch has this. Their editor uses four spaces and sorted the keys on save:

services:
    api:
        environment:
            CACHE_TTL: 300
            LOG_LEVEL: debug
        image: shop/api:1.9.0
        ports:
            - "8080:8080"
            - "9090:9090"
        restart: unless-stopped

Compared raw, every line except services: is marked as changed. Run both through yq first and the diff shrinks to this:

The edits that matter
PathMainBranchChange
api.imageshop/api:1.8.2shop/api:1.9.0Modified
api.environment.LOG_LEVELinfodebugModified
api.ports8080:80808080:8080, 9090:9090Item added
api.environment.CACHE_TTL"300" (string)300 (integer)Type changed
api.restartunless-stoppedunless-stoppedNo change (moved)

Four edits. The image bump was the point of the branch. The debug log level was probably left over from testing, and the extra 9090 port is the sort of thing nobody mentions in the PR description. The quotes dropped from CACHE_TTL are harmless in Compose, which passes every environment value to the container as a string, but the same edit in a config file your own service loads into a typed struct can become a startup error.

The traps that are unique to YAML

A few YAML features cause diffs that surprise people. Anchors and aliases (&name and *name) let one file repeat a value by reference while another writes it out in full; both load to the same data but read completely differently. The infamous "Norway problem" is another: unquoted no, off, and yes can parse as booleans in older YAML 1.1 parsers, so country: NO might become false. YAML 1.2 fixed the schema, but plenty of tools still ship 1.1 behavior. When a value looks like it changed type, that is the first thing to check.

Everyday mistakes in YAML diffs

GotchaWhy it bitesFix
Tabs for indentationYAML forbids tabs for indentation; the file may not even parseConvert tabs to spaces first
Quoted vs unquoted scalars"true" is a string, true is a booleanThis can be a real change, do not dismiss it
Anchors and aliasesOne file inlines a value, the other references itResolve anchors, then compare the expanded form
The Norway problemno can parse as false in YAML 1.1Quote ambiguous strings; check your parser version
Trailing whitespaceInvisible spaces after a value show as a changeTrim trailing whitespace before comparing

YAML and JSON are closer than they look

Every JSON document is valid YAML, because YAML 1.2 is a superset of JSON. That is handy for comparing: if the indentation or anchors are fighting you, convert both files to JSON, format them the same way, and diff that instead. Many parsers, including PyYAML, will round-trip YAML to a plain data structure you can re-emit as JSON, which strips away the stylistic differences and leaves only the data.

Related tools

YAML rarely travels alone. If you are comparing the JSON form of the same data, JSON compare can format both sides and sort the keys for you. Environment settings and .env files line up well on the config compare page, and reviewing changes between two API calls is what the API response diff is built for. Release notes and changelogs that sit next to the manifests compare best in the plain online text compare tool.

Frequently asked questions

Is it safe to paste a Kubernetes manifest or CI config into an online YAML diff?
On comparetext.org, yes. The YAML compare page diffs the two files with JavaScript in your browser tab, and nothing is stored or sent anywhere unless you press Share to create a link. Secrets are still secrets, though: if a manifest has real tokens or passwords inline, replace them with placeholders before you paste it into any web page, including ours.
Why does my YAML diff show every line as changed?
Usually one file was re-indented (two spaces versus four) or saved by an editor that sorts keys, so every line moved even though the data is the same. Run both files through yq -P with sort_keys(..) to give them identical indentation and key order, then compare again. What is left is the set of values that really changed, often just a few lines.
Does indentation width matter when comparing YAML?
Not for meaning, only for consistency. YAML uses indentation to show structure, but it does not care whether you use two spaces or four, as long as each level is consistent within the file. So a two-space file and a four-space file can hold identical data while looking entirely different to a text diff. Re-emitting both with the same tool removes that false difference. Tabs, however, are not allowed for indentation at all.
How do I compare YAML while ignoring key order?
Sort the keys before you compare. YAML mappings are unordered, so two files with the same keys in a different order hold the same data, but a text diff cannot know that. The comparetext.org YAML page compares text as written and does not reorder keys, so run yq -P with sort_keys(..) on both files first, or use a YAML-aware tool such as dyff. Leave list items in their original order, because sequence order is meaningful.
What is the Norway problem in YAML?
It is a classic YAML gotcha where the unquoted value NO is parsed as the boolean false instead of the string "NO", so a country code for Norway becomes false. It happens in YAML 1.1 parsers, which treat yes, no, on, and off as booleans. YAML 1.2 narrowed the rules, but many tools still ship 1.1 behavior. If a value looks like it flipped from a word to true or false in your diff, an unquoted boolean-like string is the likely cause. Quoting the value fixes it.
How big a YAML file can I compare in the browser?
A few thousand lines is fine; a long Helm chart output or a big OpenAPI spec compares as you type. The page lines files up line by line before looking at individual characters, which keeps it quick. Once you are into multi-megabyte dumps of a whole cluster, a terminal tool like dyff or git diff is the better choice, since it does not have to render both files on screen.

Got two versions of a config? Normalize them with yq, then paste both into the YAML compare tool.