Post

How to deduplicate elements using jq

Practical guide: how to deduplicate elements using jq.

How to deduplicate elements using jq

Input file

The following YAML file contains a list with duplicate entries that we want to identify.

1
2
3
4
5
6
7
8
some:
  awesome:
    members:
      - green
      - yellow
      - blue
      - red
      - green

Deduplication

Use yq to convert the YAML to JSON, then pipe it to jq with group_by(.) to group identical elements together. The select(length>1) filter keeps only groups with duplicates, and .[0] picks one representative from each group.

1
2
3
4
5
[arch:devopsinuse main()] yq -o=json eval  /tmp/deduplicate.yaml | jq '.some.awesome.members  | group_by(.) | map(select(length>1) | .[0])'

[
  "green"
]
This post is licensed under CC BY 4.0 by the author.