Parsing YAML files using yq

2 minute read

Looking for a lightweight tool to parse YAML files? Look no furtherโ€ฆ
I recently bumped into yq, which is command-line YAML processor that is able to traverse YAML, sort by keys, update values, export to JSON, and much moreโ€ฆ

  yq [flags]
  yq [command]

Available Commands:
  eval             Apply expression to each document in each yaml file given in sequence
  eval-all         Loads _all_ yaml documents of _all_ yaml files and runs expression once
  help             Help about any command
  shell-completion Generate completion script

  -C, --colors        force print with colors
  -e, --exit-status   set exit status if there are no matches or null or false is returned
  -h, --help          help for yq
  -I, --indent int    sets indent level for output (default 2)
  -i, --inplace       update the yaml file inplace of first yaml file given.
  -M, --no-colors     force print with no colors
  -N, --no-doc        Don't print document separators (---)
  -n, --null-input    Don't read input, simply evaluate the expression given. Useful for creating yaml docs from scratch.
  -P, --prettyPrint    pretty print, shorthand for '... style = ""' 
  -j, --tojson        output as json. Set indent to 0 to print json in one line.
  -v, --verbose       verbose mode
  -V, --version       Print version information and quit

Use "yq [command] --help" for more information about a command.



I suggest using a package manager like Chocolatey:

choco install yq

Linux / macOS

Use Homebrew to install on Linux or macOS:

brew install yq


Sample fruits.yml file:

  apple: red
  pear: green
  banana: yellow

The eval or e command, followed by the path to a YAML file will print out its entire contents:

yq e fruits.yml
  apple: red
  pear: green
  banana: yellow

Target a specific key:

yq e '' fruits.yml

Update file

You can also manipulate files directly:

yq e ' = "orange"' -i fruits.yml

โš ๏ธ If youโ€™re running yq in PowerShell and run into a parsing error, you might need to escape the quotation marks with a backslash: yq e ' = \"orange"' -i fruits.yml

Will update fruits.yml:

  apple: orange ๐Ÿ‘ˆ
  pear: green
  banana: yellow

Using variables

You can use variable operators and use them to update a key:

color="green" yq e '.fruits.banana = env(color)' -i fruits.yml
  apple: red
  pear: green
  banana: green ๐Ÿ‘ˆ

Taking things further, you could save the output of a command and assign it to the environment variable:

colorArray=("purple" "blue" "cyan")
color=${colorArray[1]} yq e '.fruits.pear = env(color)' -i fruits.yml
  apple: red
  pear: blue ๐Ÿ‘ˆ
  banana: yellow
๐Ÿ“š Found this article useful? Consider supporting this blog ๐Ÿ‘‡

Tags: , ,