Skip to content

yaml diff Examples

William W. Kimball, Jr., MBA, MSIS edited this page Oct 28, 2020 · 3 revisions
  1. Introduction
  2. Synchronizing Array Elements
    1. Order-Centric Element Comparisons
    2. Data-Centric Element Comparisons

Introduction

This page explores various real-world use-cases for the yaml-diff command-line tool.

Synchronizing Array Elements

It is sometimes more informative to compare Arrays by the distinctiveness of their elements rather than the order of them. Consider the following differences.

File: lhs.yaml

---
- alpha
- mu
- psi
- beta
- delta
- chi
- delta
- gamma
- alpha

File: rhs.yaml

---
- zeta
- mu
- psi
- alpha
- gamma
- gamma
- phi
- beta
- chi

Order-Centric Element Comparisons

By default, Array element are compared by the order in which they appear. For the sample documents, this produces the following differences:

c [0]
< alpha
---
> zeta

c [3]
< beta
---
> alpha

c [4]
< delta
---
> gamma

c [5]
< chi
---
> gamma

c [6]
< delta
---
> phi

c [7]
< gamma
---
> beta

c [8]
< alpha
---
> chi

Data-Centric Element Comparisons

When using the --arrays=value (-A value) option, this output turns very different. Rather than comparing each element, one-by-one in the order they appear within both Arrays, the elements are instead rearranged to match them together as much as possible. Doing so produces this output:

d [4]
< delta

d [6]
< delta

d [8]
< alpha

a [9]
> zeta

a [10]
> gamma

a [11]
> phi

Note once the elements are synchronized, there are actually far fewer differences between the two documents. By comparing the elements by value rather than position, we can see which elements are unique between the two Arrays.

Clone this wiki locally