yq is a portable command-line YAML, JSON, XML, CSV, TOML, HCL and properties processor
yq is a portable command-line YAML, JSON, XML, CSV, TOML, HCL and properties processor
A lightweight and portable command-line YAML, JSON, INI and XML processor. yq uses jq (a popular JSON processor) like syntax but works with yaml files as well as json, kyaml, xml, ini, 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.
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 and Podman, all listed below.
Read a value:
yq '.a.b[0].c' file.yamlPipe from STDIN:
yq '.a.b[0].c' < file.yamlUpdate a yaml file in place:
yq -i '.a.b[0].c = "cool"' file.yamlUpdate using environment variables:
NAME=mike yq -i '.a.b[0].c = strenv(NAME)' file.yamlMerge multiple files:
# merge two files
yq -n 'load("file1.yaml") * load("file2.yaml")'
# merge using globs (note: `ea` evaluates all files at once instead of in sequence)
yq ea '. as $item ireduce ({}; . * $item )' path/to/*.ymlMultiple updates to a yaml file:
yq -i '
.a.b[0].c = "cool" |
.x.y.z = "foobar" |
.person.name = strenv(NAME)
' file.yamlFind and update an item in an array:
# Note: requires input file - add your file at the end
yq -i '(.[] | select(.name == "foo") | .address) = "12 cat st"' data.yamlConvert between formats:
# Convert JSON to YAML (pretty print)
yq -Poy sample.json
# Convert YAML to JSON
yq -o json file.yaml
# Convert XML to YAML
yq -o yaml file.xmlSee recipes for more examples and the documentation for more information.
Take a look at the discussions for common questions, and cool ideas
Use wget to download pre-compiled binaries. Choose your platform and architecture:
For Linux (example):
# Set your platform variables (adjust as needed)
VERSION=v4.2.0
PLATFORM=linux_amd64
# Download compressed binary
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/yq_${PLATFORM}.tar.gz -O - |\
tar xz && sudo mv yq_${PLATFORM} /usr/local/bin/yq
# Or download plain binary
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/yq_${PLATFORM} -O /usr/local/bin/yq &&\
chmod +x /usr/local/bin/yqLatest version (Linux AMD64):
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq &&\
chmod +x /usr/local/bin/yqAvailable platforms: linux_amd64, linux_arm64, linux_arm, linux_386, darwin_amd64, darwin_arm64, windows_amd64, windows_386, etc.
Using Homebrew
brew install yqsnap install yqyq installs with strict confinement in snap, this means it doesn't have direct access to root files. To read root files you can:
sudo cat /etc/myfile | yq '.a.path'And to write to a root file you can either use sponge:
sudo cat /etc/myfile | yq '.a.path = "value"' | sudo sponge /etc/myfileor write to a temporary file:
sudo cat /etc/myfile | yq '.a.path = "value"' | sudo tee /etc/myfile.tmp
sudo mv /etc/myfile.tmp /etc/myfile
rm /etc/myfile.tmp# Docker - process files in current directory
docker run --rm -v "${PWD}":/workdir mikefarah/yq '.a.b[0].c' file.yaml
# Podman - same usage as Docker
podman run --rm -v "${PWD}":/workdir mikefarah/yq '.a.b[0].c' file.yamlSecurity note: You can run yq in Docker with restricted privileges:
docker run --rm --security-opt=no-new-privileges --cap-drop all --network none \
-v "${PWD}":/workdir mikefarah/yq '.a.b[0].c' file.yamlYou'll need to pass the -i --interactive flag to Docker/Podman:
# Process piped data
docker run -i --rm mikefarah/yq '.this.thing' < myfile.yml
# Same with Podman
podman run -i --rm mikefarah/yq '.this.thing' < myfile.ymldocker run --rm -it -v "${PWD}":/workdir --entrypoint sh mikefarah/yqpodman run --rm -it -v "${PWD}":/workdir --entrypoint sh mikefarah/yqIt can be useful to have a bash function to avoid typing the whole docker command:
yq() {
docker run --rm -i -v "${PWD}":/workdir mikefarah/yq "$@"
}yq() {
podman run --rm -i -v "${PWD}":/workdir mikefarah/yq "$@"
}yq's container image no longer runs under root (https://github.com/mikefarah/yq/pull/860). If you'd like to install more things in the container image, or you're having permissions issues when attempting to read/write files you'll need to either:
docker run --user="root" -it --entrypoint sh mikefarah/yqpodman run --user="root" -it --entrypoint sh mikefarah/yqOr, in your Dockerfile:
FROM mikefarah/yq
USER root
RUN apk add --no-cache bash
USER yqBy default, the alpine image yq uses does not include timezone data. If you'd like to use the tz operator, you'll need to include this data:
FROM mikefarah/yq
USER root
RUN apk add --no-cache tzdata
USER yqIf you are using podman with SELinux, you will need to set the shared volume flag :z on the volume mount:
-v "${PWD}":/workdir:z - name: Set foobar to cool
uses: mikefarah/yq@master
with:
cmd: yq -i '.foo.bar = "cool"' 'config.yml'
- name: Get an entry with a variable that might contain dots or spaces
id: get_username
uses: mikefarah/yq@master
with:
cmd: yq '.all.children.["${{ matrix.ip_address }}"].username' ops/inventories/production.yml
- name: Reuse a variable obtained in another step
run: echo ${{ steps.get_username.outputs.result }}See https://mikefarah.gitbook.io/yq/usage/github-action for more.
go install github.com/mikefarah/yq/v4@latestAs these are supported by the community :heart: - however, they may be out of date with the officially supported releases.
Please note that the Debian package (previously supported by @rmescandon) is no longer maintained. Please use an alternative installation method.
Checkout yq on x-cmd: https://x-cmd.com/mod/yq
Thanks @edwinjhlee!
nix profile install nixpkgs#yq-goSee here
webi yqSee webi Supported by @adithyasunil26 (https://github.com/webinstall/webi-installers/tree/master/yq)
pacman -S go-yqUsing Chocolatey
choco install yqSupported by @chillum (https://chocolatey.org/packages/yq)
Using scoop
scoop install main/yqUsing winget
winget install --id MikeFarah.yqUsing MacPorts
sudo port selfupdate
sudo port install yqSupported by @herbygillot (https://ports.macports.org/maintainer/github/herbygillot)
Alpine Linux v3.20+ (and Edge):
apk add yq-goAlpine Linux up to v3.19:
apk add yqSupported by Tuan Hoang (https://pkgs.alpinelinux.org/packages?name=yq-go)
Flox can be used to install yq on Linux, MacOS, and Windows through WSL.
flox install yqUsing gah
gah install yqjq but works with YAML, INI, JSON and XML filesCheck out the documentation for more detailed and advanced usage.
…PowerShell quoting issues:
# Use single quotes for expressions
yq '.a.b[0].c' file.yaml
# Or escape double quotes
yq ".a.b[0].c = \"value\"" file.yamlyq attempts to preserve comment positions and whitespace as much as possible, but it does not handle all scenarios (see https://github.com/go-yaml/yaml/tree/v3 for details)sort and sort_by silently accept types with undefined ordering
Binary integers (0b101) are tagged !!int but fail to parse
TOML parsing & conversion does not preserve object order
Single-quoted multi-line flow scalar ending with a blank line loses indentation level on output
RFE: ability to specify input filename for stdin
Parsing error with <unknown position> as line / column or bad line
Unexpected INI parsing of keys with colon
sort_keys(..) doesn't preserve multi-line string format with | for a long value
String Interpolation Acts on Unselected Documents
asymmetry between map_values and map (inconsistent with jq)