jq @base64 : Base64-Encode in a Pipeline
jq @base64 base64-encodes a string and @base64d decodes one, all inside the filter.
The GitHub contents API returns file bodies base64-encoded; @base64d decodes them in the same jq call that fetches them.
What it does
@base64 is a format string that base64-encodes the string it is applied to; @base64d decodes. Used in interpolation like "\(.x | @base64)" or applied directly. Non-string inputs are first serialized to JSON text.
Common usage
# decode a file body from the contents API
gh api repos/cli/cli/contents/README.md --jq '.content | @base64d'
# encode a value
echo '"hello"' | jq -r '@base64'
# decode inside string interpolation
jq -r '"\(.content | @base64d)"' file.jsonFormats
| Format | What it does |
|---|---|
| @base64 | Base64-encode the string |
| @base64d | Base64-decode the string |
| @uri | Percent-encode for a URL |
| -r | Print the decoded string without quotes |
In CI
Add -r so the decoded text is emitted raw rather than as a quoted JSON string. The contents API often wraps base64 at 60 columns; jq @base64d handles the embedded newlines, unlike some shell base64 -d invocations.
Common errors in CI
"jq: error: <stdin>:0: @base64d: invalid base64" means the input was not valid base64, often because a non-string value was passed and serialized first. "jq: error: Cannot index string with \"content\"" means you indexed .content on something that was already a string.