Package: yq
Installationpip install yqBefore usingyq, you also have to install its dependency,jq. See thejq installation instructionsfor details and directions specific to your platform.On macOS,yqis also available onHomebrew: usebrew installpython-yq.Synopsisyqtakes YAML input, converts it to JSON, and pipes it tojq:cat input.yml | yq .foo.barLike injq, you can also specify input filename(s) as arguments:yq .foo.bar input.ymlBy default, no conversion ofjqoutput is done. Use the--yaml-output/-yoption to convert it back into YAML:cat input.yml | yq -y .foo.barMapping key order is preserved. By default, customYAML tagsandstylesin the input are ignored. Use the--yaml-roundtrip/-Yoption 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.ymlyq can be called as a module if needed. With-y/-Y, files can be edited in place like withsed-i:python -m yq -Y --indentless --in-place '.["current-context"] = "staging-cluster"' ~/.kube/configUse the--width/-woption to pass the line wrap width for string literals. Use--explicit-start/--explicit-endto emit YAML start/end markers even when processing a single document. All other
command line arguments are forwarded tojq.yqforwards the exit codejqproduced, unless there was an error
in YAML parsing, in which case the exit code is 1. See thejq manualfor more
details onjqfeatures 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.Preserving tags and styles using the-Y(--yaml-roundtrip) optionThe-Yoption helps preserve customstring stylesandtagsin your document. For example, consider the following
document (anAWS CloudFormationtemplate fragment):Resources:
  ElasticLoadBalancer:
    Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
    Properties:
      AvailabilityZones: !GetAZs ''
      Instances:
        - !Ref Ec2Instance1
        - !Ref Ec2Instance2
      Description: >-
        Load balancer for Big Important Service.

        Good thing it's managed by this template.Passing this document throughyq-y.Resources.ElasticLoadBalancerwill drop custom tags, such as!Ref,
and styles, such as thefoldedstyle of theDescriptionfield:Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
  AvailabilityZones: ''
  Instances:
    - Ec2Instance1
    - Ec2Instance2
  Description: 'Load balancer for Big Important Service.

    Good thing it''s managed by this template.'By contrast, passing it throughyq-Y.Resources.ElasticLoadBalancerwill preserve tags and styles:Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
Properties:
  AvailabilityZones: !GetAZs ''
  Instances:
    - !Ref 'Ec2Instance1'
    - !Ref 'Ec2Instance2'
  Description: >-
    Load balancer for Big Important Service.

    Good thing it's managed by this template.To accomplish this in-Ymode, yq carries extra metadata (mapping pairs and sequence values) in the JSON
representation of your document for any custom tags or styles that it finds. When converting the JSON back into YAML, it
parses this metadata, re-applies the tags and styles, and discards the extra pairs and values.WarningThe-Yoption is incompatible with jq filters that do not expect the extra information injected into the document
to preserve the YAML formatting. For example, a jq filter that counts entries in the Instances array will come up with
4 entries instead of 2. A filter that expects all array entries to be mappings may break due to the presence of string
metadata keys. Check your jq filter for compatibility/semantic validity when using the-Yoption.XML supportyqalso supports XML. Theyqpackage installs an executable,xq, whichtranscodes XML to JSONusingxmltodictand pipes it tojq. Roundtrip transcoding is available with
thexq--xml-output/xq-xoption. Multiple XML documents can be passed in separate files/streams asxq a.xml b.xml. Use--xml-item-depthto descend into large documents, streaming their contents without loading
the full doc into memory (for example, stream aWikipedia database dumpwithcatenwiki-*.xml.bz2| bunzip2 | xq .--xml-item-depth=2). Entity expansion and DTD resolution is disabled to avoid
XML parsing vulnerabilities.TOML supportyqsupportsTOMLas well. Theyqpackage installs an executable,tomlq, which uses thetomlkit libraryto transcode TOML to JSON, then pipes it tojq. Roundtrip
transcoding is available with thetomlq--toml-output/tomlq-toption.Compatibility noteThis package’s release series available on PyPI begins with version 2.0.0. Versions ofyqprior to 2.0.0 are
distributed byhttps://github.com/abesto/yqand are not related to this package. No guarantees of compatibility are
made between abesto/yq and kislyuk/yq. This package follows theSemantic Versioning 2.0.0standard. To ensure proper operation, declare dependency version ranges according to SemVer.AuthorsAndrey KislyukLinksProject home page (GitHub)DocumentationPackage distribution (PyPI)Change logjq- the command-line JSON processor utility poweringyqBugsPlease report bugs, issues, feature requests, etc. onGitHub.LicenseLicensed under the terms of theApache License, Version 2.0.