yq: Command-line YAML/XML processor - jq wrapper for YAML and XML documents

Installation

pip 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.

Synopsis

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 argument to convert it back into YAML:

cat input.yml | yq -y .foo.bar

Use the --width/-w argument to pass the line wrap width for string literals. 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.

YAML tags in the input are ignored (any nested data is treated as untagged). Key order is preserved.

XML support

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.

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.

Authors

  • Andrey Kislyuk

License

Licensed under the terms of the Apache License, Version 2.0.

https://img.shields.io/travis/kislyuk/yq.svg https://codecov.io/github/kislyuk/yq/coverage.svg?branch=master https://img.shields.io/pypi/v/yq.svg https://img.shields.io/pypi/l/yq.svg https://readthedocs.org/projects/yq/badge/?version=latest

API documentation

Release Notes

Changes for v2.7.1 (2018-11-05)

  • xq: Introduce –xml-dtd and –xml-root. Fixes #37.
  • TOML support is optional and experimental

Changes for v2.7.0 (2018-08-04)

  • TOML support with the tq executable entry point.
  • Disallow argparse abbreviated options. Fixes #38 on Python 3.5+.
  • Now available in Homebrew as python-yq.

Changes for v2.6.0 (2018-04-28)

  • Packaging: Replace scripts with entry-points
  • Packaging: Package the license file

Changes for v2.5.0 (2018-04-02)

  • Parse unrecognized tags instead of dropping them. Fixes #23

Changes for v2.4.1 (2018-02-13)

  • Ignore unrecognized YAML tags instead of crashing
  • Explicitly disable XML entity expansion and mention in docs
  • xq -x: Raise understandable error on non-dict conversion failure

Changes for v2.4.0 (2018-02-08)

  • Support XML parsing with xmltodict

Changes for v2.3.7 (2018-02-07)

  • Fix for the –from-file/-f argument: Re-route jq_filter to files when using –from-file. Fixes #19.

Changes for v2.3.6 (2018-01-29)

  • Parse and pass multiple positional-consuming jq args

Changes for v2.3.5 (2018-01-29)

  • Support jq arguments that consume subsequent positionals (such as –arg k v) (#16).

Changes for v2.3.4 (2017-12-26)

  • Support bare YAML dates and times. Fixes #10

Changes for v2.3.3 (2017-09-30)

  • Avoid buffering all input docs in memory with no -y
  • End all json.dump output with newlines. Close all input streams. Fixes #8. Thanks to @bubbleattic for reporting.

Changes for v2.3.2 (2017-09-25)

  • Fix test suite on Python 3

Changes for v2.3.1 (2017-09-25)

  • Add support for multiple yaml files in arguments. Thanks to @bubbleattic (PR #7)

Changes for v2.3.0 (2017-08-27)

  • Handle multi-document streams. Fixes #6
  • Report version via yq –version

Changes for v2.2.0 (2017-07-07)

  • Stringify datetimes loaded from YAML. Fixes #5

Changes for v2.1.2 (2017-06-27)

  • Fix ResourceWarning: unclosed file
  • Internal: Make usage of loader argument consistent
  • Documentation improvements

Changes for v2.1.1 (2017-05-02)

  • Fix release script. Release is identical to v2.1.0.

Changes for v2.1.0 (2017-05-02)

  • yq now supports emitting YAML (round-trip YAML support) using “yq -y”. Fixes #2.
  • Key order is now preserved in mappings/objects/dictionaries.
  • Passing input files by filename as an argument is now supported (in addition to providing data on standard input).

Changes for v2.0.2 (2017-01-16)

  • Test and documentation improvements

Changes for v2.0.1 (2017-01-14)

  • Fix description in setup.py

Changes for v2.0.0 (2017-01-14)

  • Begin 2.0.0 release series. 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 <http://semver.org/>_ standard. To ensure proper operation, declare dependency version ranges according to SemVer. See the Readme for more information.

Table of Contents