Skip to main content

Build Your Own yq

The command line tool yq is like jq for YAML data - you can use it to and filter and transform YAML data, much like you would JSON dats with jq. By the way, a past Coding Challenge was to build your own jq if you fancy giving that a go.

Step Zero

Like yq itself we’re zero-indexed! In this step you’re going to set your environment up ready to begin developing our yq clone: ccyq.

You can download some test YAML from my dropbox here.

Step 1

In this step your goal is to open the yaml file, read it and print it. You could parse it at this point or just read it and print it line by line.

The outcome should look something like this:

% ccyq quotes.yaml
quotes:
  - id: 1
    quote: It is bad for a young man to sin; but it is worse for an old man to sin.
    author: Abu Bakr (R.A)
<snip>
  - id: 5
    quote: >-
      These Capitalists Generally Act Harmoniously And In Concert, To Fleece The People.
    author: Abraham Lincoln

N.B. I’ve trimmed the output to just show the first and last quote.

Step 2

In this step your goal is to handle the object identifier: .quotes and .[<string>], as well as the optional (?) extension to it.

The commands you want to support look like this:

% ccyq '.[quotes]' quotes.yaml
% ccyq '.["quotes"]' quotes.yaml
% ccyq '.quotes?' quotes.yaml
% ccyq '.["quotes"]?' quotes.yaml

Don’t forget to add to the YAML file to ensure that only the quotes are extracted.

Step 3

In this step your goal is to support the array index: .[<number>]. Like most programming languages, arrays in yq are zero indexed.

In use that would look something like this:

% ccyq '.quotes[0]' quotes.yaml
id: 1
quote: It is bad for a young man to sin; but it is worse for an old man to sin.
author: Abu Bakr (R.A)

Don’t forget to test other index values, including those that are out of range.

Step 4

In this step your goal is to support the pipe operator ‘|’. The pipe operator combines two filters by feeding the output of the one on the left into the one on the right. It's similar to the Unix shell's pipe. Here’s an example command:

% ccyq '.quotes[1] | .quote' quotes.yaml
If You Are Out To Describe The Truth, Leave Elegance To The Tailor.

Step 5

In this step your goal is to collect the output as an array. You can do this by wrapping the filter in square brackets, that would look like this:

% ccyq '[.quotes[].quote]' quotes.yaml
- It is bad for a young man to sin; but it is worse for an old man to sin.
- If You Are Out To Describe The Truth, Leave Elegance To The Tailor.
- >-
  O man you are busy working for the world, and the world is busy trying to turn you out.
- >-
  While children are struggling to be unique, the world around them is trying all means to make them look like everybody else.
- >-
  These Capitalists Generally Act Harmoniously And In Concert, To Fleece The People.

Going Further

There are many other options available in yq itself, if you have the time you can read about in yq’s documentation and implement as many of them as possible.

Help Others by Sharing Your Solutions!

If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server or in the Coding Challenges Sub Reddit, via Twitter or LinkedIn or just post about it there and tag me.

Get The Challenges By Email

If you would like to recieve the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here: