Zeljko's Blog

TIL: YQ Is Powerful

Created: 2023-03-13
Last Modified: 2025-01-29

I came across the [yq] command and I enjoy using it. It’s like [jq], but for YAML. But not really because it accepts also JSON and XML as inputs too. I tend to use it to decode the data of kubernetes secrets. yq makes very short work of it.

kubectl get secrets mySecret | yq '.data | map_values(@base64d)'

You could of course go the way to output the .stringData field of the secret if it exists too. Also, it’s nice for splitting resources yq-split. This is useful, when you want to get a list of resources from kubernetes for example with kubectl get pods -o yaml, and then you want to process the manifests separately as files.

Another usage of yq for me is parsing extracting data from an HTML. It sounds strange, but setting the field to HTML and then fiddling around with the path works very well. Lastly, I don’t output it as YAML but JSON. This is because I wanted to do more processing of this data in python. But why didn’t I just use requests and beautifulsoup? I wanted a simple solution. I didn’t want to install them. Also, the HTML which I got was invalid, and HTML parser had problems with it. yq somehow managed to get the data how I want it.

Thank you for reading.