pip3 install yq
Before using yq
, you also have to install its dependency, jq
. See the jq installation instructions for details and directions specific to your platform.
On macOS, yq
is also available on Homebrew: use brew install python-yq
.
yq
takes YAML input, converts it to JSON, and pipes it to jq:
cat input.yml | yq .foo.bar
Like in jq
, you can also specify input filename(s) as arguments:
yq .foo.bar input.yml
By default, no conversion of jq
output is done. Use the --yaml-output
/-y
option to convert it back into YAML:
cat input.yml | yq -y .foo.bar
Mapping key order is preserved. By default, custom YAML tags and
styles in the input are ignored. Use the --yaml-roundtrip
/-Y
option to preserve YAML tags and styles by representing them as extra items in their enclosing mappings and sequences
while in JSON:
yq -Y .foo.bar input.yml
Use the --width
/-w
option to pass the line wrap width for string literals. With -y/-Y
, files can be edited
in place like with sed -i
: yq -yi .foo=1 *.yml
. All other command line arguments are forwarded to jq
. yq
forwards the exit code jq
produced, unless there was an error in YAML parsing, in which case the exit code is 1.
See the jq manual for more details on jq
features and options.
Because YAML treats JSON as a dialect of YAML, you can use yq to convert JSON to YAML: yq -y . < in.json > out.yml
.
yq
also supports XML. The yq
package installs an executable, xq
, which
transcodes XML to JSON using
xmltodict and pipes it to jq
. Roundtrip transcoding is available with
the xq --xml-output
/xq -x
option. Multiple XML documents can be passed in separate files/streams as
xq a.xml b.xml
. Entity expansion and DTD resolution is disabled to avoid XML parsing vulnerabilities.
yq
supports TOML as well. The yq
package installs an executable, tomlq
, which uses the
toml library to transcode TOML to JSON, then pipes it to jq
. Roundtrip transcoding
is available with the tomlq --toml-output
/tomlq -t
option.
Compatibility note
This package’s release series available on PyPI begins with version 2.0.0. Versions of yq
prior to 2.0.0 are
distributed by https://github.com/abesto/yq and are not related to this package. No guarantees of compatibility are
made between abesto/yq and kislyuk/yq. This package follows the Semantic Versioning 2.0.0
standard. To ensure proper operation, declare dependency version ranges according to SemVer.
jq - the command-line JSON processor utility powering yq
Please report bugs, issues, feature requests, etc. on GitHub.
Licensed under the terms of the Apache License, Version 2.0.
usage: yq [options] <jq filter> [input file...]
[--indentless-lists] [--in-place] [--version]
[jq_filter] [files [files ...]]
yq: Command-line YAML processor - jq wrapper for YAML documents
yq transcodes YAML documents to JSON and passes them to jq.
See https://github.com/kislyuk/yq for more information.
positional arguments:
jq_filter
files
optional arguments:
-h, --help show this help message and exit
--yaml-output, --yml-output, -y
Transcode jq JSON output back into YAML and emit it
--yaml-roundtrip, --yml-roundtrip, -Y
Transcode jq JSON output back into YAML and emit it. Preserve YAML tags and styles by representing them as extra items in their enclosing mappings and sequences while in JSON. This option is incompatible with jq filters that do not expect these extra items.
--yaml-output-grammar-version {1.1,1.2}, --yml-out-ver {1.1,1.2}
When using --yaml-output, specify output grammar (the default is 1.1 and will be changed to 1.2 in a future version). Setting this to 1.2 will cause strings like 'on' and 'no' to be emitted unquoted.
--width WIDTH, -w WIDTH
When using --yaml-output, specify string wrap width
--indentless-lists, --indentless
When using --yaml-output, indent block style lists (sequences) with 0 spaces instead of 2
--in-place, -i Edit files in place (no backup - use caution)
--version show program's version number and exit
jq - commandline JSON processor [version 1.6]
Usage: /Users/andrey.kislyuk/.asdf/installs/jq/1.6/bin/jq [options] <jq filter> [file...]
/Users/andrey.kislyuk/.asdf/installs/jq/1.6/bin/jq [options] --args <jq filter> [strings...]
/Users/andrey.kislyuk/.asdf/installs/jq/1.6/bin/jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.
The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).
For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq
Example:
$ echo '{"foo": 0}' | jq .
{
"foo": 0
}
Some of the options include:
-c compact instead of pretty-printed output;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
--rawfile a f set variable $a to a string consisting of the contents of <f>;
--args remaining arguments are string arguments, not files;
--jsonargs remaining arguments are JSON arguments, not files;
-- terminates argument processing;
Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].
See the manpage for more options.