Skip to content
Latchkey

yq -o=json: Convert YAML to JSON

yq -o=json reads YAML and prints it as JSON, so you can feed YAML configs to tools that want JSON.

Many tools and APIs only accept JSON. yq converts a YAML manifest to JSON in one step, with control over indentation and document handling.

What it does

yq -o=json (or --output-format=json) serializes the parsed document as JSON instead of YAML. Anchors and aliases are expanded; comments are dropped (JSON has none). -I sets the indent; -I=0 produces compact one-line JSON.

Common usage

Terminal
yq -o=json '.' deploy.yaml
yq -o=json -I=0 '.' deploy.yaml          # compact single line
yq -o=json '.spec' deploy.yaml | jq '.replicas'

Output options

FlagWhat it does
-o=json / --output-format=jsonEmit JSON instead of YAML
-I=2 / --indent=2Set indentation width
-I=0Compact, single-line JSON
-o=props / -o=csv / -o=tsvOther output formats
... comments=""Drop comments first (JSON cannot hold them)

In CI

To pass a manifest value to a JSON-only API: yq -o=json -I=0 '.spec.config' app.yaml then POST it. For a single value into $GITHUB_OUTPUT, the compact form avoids embedded newlines that would break the key=value contract.

Common errors in CI

A multi-document YAML file emits multiple JSON objects (not a JSON array), which downstream JSON parsers reject; wrap with -o=json on eval-all and [...] or select a single document first. "Error: ... !!binary" or unusual tags can fail JSON conversion. With Python yq the direction is reversed: it always emits JSON by default and needs -y to keep YAML, so "I got JSON I did not ask for" often means the wrong yq.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →