The jq tool was a game-changer for JSON on the command line. Before jq manipulating JSON data meant invoking programs in third-party languages. jq changed that for the better. But the world has moved on, more and more people have adopted YAML. There was a need for a YAML processing jq-like tool—the yq.
There were multiple attempts to solve this problem, often sharing the same name.
The yq from Mike Farah is the best one.
The MIT-licensed yq source code is available on GitHub[1].
table of contents
§what is yq
After GitHub repository:
yq: a lightweight and portable command-line YAML, JSON and XML processor. yq uses jq like syntax but works with yaml files as well as json, xml, properties, csv and tsv. It doesn’t yet support everything jq does - but it does support the most common operations and functions, and more is being added continuously.
§how do I install yq
yq is written in go. If you have go installed, the easiest method is:
|
|
If you don’t have go installed, no worries. There are plenty of other ways to install the tool, simply consult the GitHub repository[2].
§how do I use it
The documentation is thorough[3]. The best way to start is to check examples and simply play with it.
There are many easy to find yq tutorials so I’ll spare that here.
However, I’ll share some pretty cool teasers.
§yq teasers
§extracting all helm chart dependencies with versions
|
|
file://../testkube-operator@1.13.0
https://charts.bitnami.com/bitnami@12.1.31
https://nats-io.github.io/k8s/helm/charts/@0.19.1
file://../testkube-api@1.13.0
file://../testkube-dashboard@1.13.0
file://../global@0.1.1
§extracting only non-local helm chart dependencies with versions
|
|
https://charts.bitnami.com/bitnami@12.1.31
https://nats-io.github.io/k8s/helm/charts/@0.19.1
§extracting default images from helm chart values
|
|
"mongodb-12.1.31" has been added to your repositories
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "mongodb-12.1.31" chart repository
Update Complete. ⎈Happy Helming!⎈
x mongodb/Chart.yaml
...
x mongodb/charts/common/README.md
/Users/radek/dev
"mongodb-12.1.31" has been removed from your repositories
MongoDB image is: docker.io/bitnami/mongodb:5.0.10-debian-11-r3
§count number of CRDs
|
|
14
§find latest non-literal-latest ory hydra image tag in docker hub
|
|
v2.2.0
§find latest numerical keycloak image tag available in quay.io
|
|
21.1
§find unique statefulset images used by the yugabytedb helm chart
|
|
yugabytedb/yugabyte:2.19.0.0-b190
|
|
§apply selected resources only
|
|
§closing words
yq brings new superpowers to the terminal. Processing YAML, JSON, XML, TSV, and CSV with one tool is absolutely outstanding.
A real must-have tool in modern shell data processing stack.