Querying GitHub API and gh CLI Output With fx
The GitHub REST API and the gh CLI both return JSON. Pipe it into fx to browse it interactively or to extract, filter, and sort fields with short JavaScript expressions. If you don't have it yet, install fx.
View GitHub API JSON in the Terminal
Pipe any gh api call into fx to open the response in the interactive viewer:
gh api repos/antonmedv/fx | fxCollapse and expand nodes with the arrow keys, press / to search, or @ to fuzzy-search paths. To just pretty-print the response without opening the viewer, pass .:
gh api repos/antonmedv/fx | fx .Extract Fields From a GitHub API Response
Get a single field with .field:
gh api repos/antonmedv/fx | fx '.stargazers_count'20145Build a smaller object from the fields you care about, including nested ones:
gh api repos/antonmedv/fx | fx '({name: x.full_name, stars: x.stargazers_count, forks: x.forks_count, license: x.license.spdx_id})'{
"name": "antonmedv/fx",
"stars": 20145,
"forks": 459,
"license": "MIT"
}Print an array one item per line with list:
gh api repos/antonmedv/fx | fx '.topics' listcli
json
terminal
tuiParse gh CLI JSON Output
Most gh commands accept --json with a list of fields. Map over the result with @:
gh pr list --json number,title,author,isDraft,labels | fx '@.title'[
"Add TOML output support",
"Bump golang.org/x/term from 0.24.0 to 0.25.0",
"WIP: fuzzy search improvements",
"Fix crash on empty input with --slurp"
]Filter Pull Requests by Author, Draft, or Label
Use ? to filter. This drops bot PRs and drafts, then formats each PR as a line:
gh pr list --json number,title,author,isDraft | fx '?!x.author.is_bot && !x.isDraft' '@`#${x.number} ${x.title} (@${x.author.login})`' list#412 Add TOML output support (@antonmedv)
#405 Fix crash on empty input with --slurp (@mkim)Find PRs with a specific label:
gh pr list --json number,labels | fx '?.labels.some(l => l.name === "bug")' '@.number'[
405
]List a User's GitHub Repos Sorted by Stars
Fetch repos from the REST API with curl, skip forks, and sort by stargazers_count in descending order with sortBy:
curl -s 'https://api.github.com/users/antonmedv/repos?per_page=100' \
| fx '?!x.fork' 'sortBy(r => -r.stargazers_count)' '@({name: x.name, stars: x.stargazers_count})'[
{ "name": "fx", "stars": 20145 },
{ "name": "expr", "stars": 6812 },
{ "name": "walk", "stars": 3290 },
{ "name": "finder", "stars": 1405 }
]For a tab-separated table:
curl -s 'https://api.github.com/users/antonmedv/repos?per_page=100' \
| fx '?!x.fork' 'sortBy(r => -r.stargazers_count)' '@`${x.stargazers_count}\t${x.full_name}`' list20145 antonmedv/fx
6812 antonmedv/expr
3290 antonmedv/walk
1405 antonmedv/finderCount Open Issues by Label
Flatten all label names with .[], group them with groupBy, and replace each group with its size:
gh issue list --state open --json number,title,labels \
| fx '.[].labels[].name' 'groupBy(x => x)' 'x => Object.fromEntries(Object.entries(x).map(([k, v]) => [k, v.length]))'{
"bug": 3,
"tui": 2,
"enhancement": 2
}Issues with no labels at all:
gh issue list --state open --json number,labels | fx '?.labels.length === 0' '@.number'[
410
]Find a Release Download URL for Your Platform
The latest release lists its files under assets. Pick one by name:
gh api repos/antonmedv/fx/releases/latest | fx '.assets.find(a => a.name.includes("linux_arm64")).browser_download_url'https://github.com/antonmedv/fx/releases/download/39.2.0/fx_linux_arm64Strings are printed without quotes, so you can pass the result straight to curl -LO "$(...)". To list every macOS build:
gh api repos/antonmedv/fx/releases/latest | fx '.assets' '?.name.includes("darwin")' '@.browser_download_url' listhttps://github.com/antonmedv/fx/releases/download/39.2.0/fx_darwin_amd64
https://github.com/antonmedv/fx/releases/download/39.2.0/fx_darwin_arm64Process Paginated gh api --paginate Output
gh api --paginate prints one JSON array per page, one after another. fx reads concatenated JSON as a stream, so each expression runs once per page:
gh api --paginate users/antonmedv/repos | fx '.length'2
2
1To work with all pages as one list, use --slurp to collect the pages into an array, then flatten it:
gh api --paginate users/antonmedv/repos | fx --slurp flatten 'sortBy(r => -r.stargazers_count)' '@.name'[
"fx",
"expr",
"walk",
"finder",
"gitmoji"
]Total stars across every page:
gh api --paginate users/antonmedv/repos | fx --slurp flatten '.reduce((sum, r) => sum + r.stargazers_count, 0)'31655TIP
Without --slurp, sorting and grouping only apply within a single page. Slurp whenever the result depends on the whole list.