Sub topic 8
Structured output with --json and --jq
This chapter covers how to get machine-readable output from gh and filter it. It assumes you can already list issues and pull requests with gh issue list and gh pr list, and that you have a basic idea of what JSON is. You do not need to know jq in advance.
Why default output is not enough
By default, gh prints tables meant for people. Column widths change with your terminal, and colors disappear when you pipe the output. Parsing that text with awk or cut breaks easily.
The --json flag fixes this. It makes the command print JSON, a text format for structured data, with the fields you choose. The --jq flag then filters that JSON using jq expressions. You do not need to install jq separately, because gh has the filter built in.
Choosing fields with --json
Run --json with no field names to see which fields a command supports:
gh pr list --jsonThe command prints an error that lists the valid fields, such as number, title, author, labels, and state. Pass a comma-separated list to select some of them:
gh pr list --json number,title,authorThe result is a JSON array with one object per pull request. Only the fields you named appear. Fields like author are nested objects, so you will often need --jq to reach the value you want.
Filtering with --jq
A jq expression describes how to transform the JSON. The expression .[] means "each element of the array". Adding .number picks one field from each element.
gh pr list --json number,title --jq '.[] | "\(.number)\t\(.title)"'This prints each open pull request's number and title on one tab-separated line. The pipe | inside the expression passes each element to the string template on its right. In the template, \(.number) inserts a value. The \t is a tab.
Because the output is plain lines, it works with other shell tools:
gh pr list --json number,title --jq '.[] | "\(.number)\t\(.title)"' | sort -nCommon jq patterns
| Goal | Expression | Column |
|---|---|---|
| One value per line | `.[].title` | |
| Nested field | `.[].author.login` | |
| Keep only matching items | `.[] \ | select(.isDraft == false)` |
| Count items | `length` | |
| Names from a list of objects | `.[].labels[].name` |
Use select to filter inside jq, not in the shell. For example, this lists the numbers of open issues that carry the bug label:
gh issue list --json number,labels --jq '.[] | select(.labels[].name == "bug") | .number'How the pieces fit together
Common errors
- Unknown field.
ghrejects a name that is not in the supported list. Run the command with a bare--jsonto see the valid names. --jqwithout--json. The filter needs JSON input, so you must give both flags.- Shell quoting. Wrap the expression in single quotes. Double quotes let the shell expand
$and backslashes beforejqsees them. - Raw output surprises. When
--jqproduces a string,ghprints it without quotes. Objects and arrays print as JSON. - Default limits.
gh pr listreturns 30 items unless you pass--limit. A filter only sees what the command returned.
Exercises
Exercise
Write a command that prints only the login of each pull request author, one per line, for up to 100 open pull requests.
Show answer
gh pr list --limit 100 --json author --jq '.[].author.login'
Exercise
Why does gh pr list --jq '.[].title' fail?
Show answer
--jq needs JSON input, and no --json flag selects any fields. Add --json title, as in gh pr list --json title --jq '.[].title'.