How to filter yaml data using yq?

How to filter yaml data using yq?

How to filter yaml data using yq?

With the increase use of API and event driven architectures with dynamic configurations mostly defined in YAML or JSON, its essential for a DevOps or Software engineer to know how to parse yaml data.

What is YAML?

YAML or Yet Another Markup Language is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Extensible Markup Language (XML) but has a minimal syntax.

YAML was first proposed by Clark Evans in 2001. Originally YAML was said to mean Yet Another Markup Language, because it was released in an era that saw a proliferation of markup languages for presentation and connectivity (HTML, XML, SGML, etc). Its initial name was intended as a tongue-in-cheek reference to the technology landscape, referencing its purpose as a markup language with the yet another construct, but it was then repurposed as YAML Ain’t Markup Language, a recursive acronym, to distinguish its purpose as data-oriented, rather than document markup.

In the cloud and DevOps space most of the configuration files are defined YAML formats. YAML is used in everywhere from your pipeline definition or configuration management suite like Ansible or containerization suites like Kubernetes.

Sample YAML data:

What is yq?

yq a lightweight and portable command-line YAML processor written by Mike Farah. yq uses jq like syntax but works with yaml files as well as json. It doesn’t yet support everything jq does – but it does support the most common operations and functions, and more is being added continuously. yq is written in go – so you can download a dependency free binary for your platform and you are good to go! If you prefer there are a variety of package managers that can be used as well as docker, all listed below.

How to install yq?

YQ is available in almost all modern operating systems and can be installed fairly easy in couple of commands. In my view tools like yq, jq, git,python should be included as part of the OS distribution.

Installing YQ in Windows OS:

Installing YQ in MAC OS:

Installing YQ in Linux OS:

How to parse YAML data using YQ?

How to add new elements in a YAML file.

How to update an existing element in YAML file?

How to delete an existing element in a YAML file?

How to check if a key or value exists in a YAML file?

Hope you have enjoyed this article. To get more details on YQ, please refer below official documentation.

https://mikefarah.gitbook.io/yq/

Leave a Reply

Close Menu