yq -p=json: Read JSON, Output YAML
yq -p=json tells yq the input is JSON; by default it then prints YAML, converting JSON to YAML.
The reverse of YAML-to-JSON: take a JSON config or API response and turn it into readable YAML, or just query it with yq path expressions.
What it does
yq -p=json (or --input-format=json) parses the input as JSON. Since the default output format is YAML, yq -p=json '.' file.json converts JSON to YAML. Pair -p=json with -o=json to query JSON and keep JSON output.
Common usage
yq -p=json '.' config.json # JSON to YAML
yq -p=json '.image.tag' config.json # query JSON with yq
yq -p=json -o=json '.spec' config.json # JSON in, JSON outInput options
| Flag | What it does |
|---|---|
| -p=json / --input-format=json | Parse input as JSON |
| -p=yaml | Parse input as YAML (the default) |
| -p=props / -p=csv / -p=tsv | Parse other input formats |
| -p=json -o=yaml | Convert JSON to YAML (output default) |
| -p=json -o=json | Query JSON, keep JSON output |
In CI
When an API returns JSON but your tooling wants YAML, curl ... | yq -p=json '.' > config.yaml does the conversion inline. Since JSON is a subset of YAML, yq can often read JSON even without -p=json, but setting it is the reliable choice.
Common errors in CI
"Error: bad file ... could not find expected" on a JSON file usually means you forgot -p=json on input that uses tabs or other JSON-only formatting YAML rejects. Getting YAML when you wanted JSON means you set -p=json but not -o=json. On Python yq the model is inverted (JSON-first, jq-based), so the -p/-o flags do not exist there.