diff --git a/README.md b/README.md index f22f65e..6130e0a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1801 TILs and counting..._ +_1802 TILs and counting..._ See some of the other learning resources I work on: @@ -470,6 +470,7 @@ If you've learned something here, support my efforts writing daily TILs by - [List PRs Awaiting Your Review](github/list-prs-awaiting-your-review.md) - [Open A PR To An Unforked Repo](github/open-a-pr-to-an-unforked-repo.md) - [Open File To Specific Line In Browser](github/open-file-to-specific-line-in-browser.md) +- [Process JSON Output From gh With jq](github/process-json-output-from-gh-with-jq.md) - [Target Another Repo When Creating A PR](github/target-another-repo-when-creating-a-pr.md) - [Tell gh What The Default Repo Is](github/tell-gh-what-the-default-repo-is.md) diff --git a/github/process-json-output-from-gh-with-jq.md b/github/process-json-output-from-gh-with-jq.md new file mode 100644 index 0000000..28794c6 --- /dev/null +++ b/github/process-json-output-from-gh-with-jq.md @@ -0,0 +1,58 @@ +# Process JSON Output From gh With jq + +The `gh` (GitHub) CLI is useful for accessing data about your profile and +projects from the terminal. With the `--json` flag, we can access the data in a +structured way which is useful for scripting. + +Here is an example of pulling a list of all my repositories, limiting each +entity to just the `nameWithOwner` and `description`: + +```bash +❯ gh repo list --limit 1000 --json nameWithOwner,description +[ + { + "description": "My personal site -- joshbranchaud.com", + "nameWithOwner": "jbranchaud/personal-site" + }, + { + "description": "Private repo for the NOTES.md of my TIL repo", + "nameWithOwner": "jbranchaud/til-notes-private" + }, + ... +] +``` + +If I'm using the `--json` flag, then I can add in the `--jq` flag to apply a +`jq` query for additional processing of the output. + +Here I convert it to a series of tuples: + +```bash +❯ gh repo list --limit 1000 --json nameWithOwner,description \ + --jq '.[] | [.nameWithOwner, .description]' +[ + "jbranchaud/personal-site", + "My personal site -- joshbranchaud.com" +] +[ + "jbranchaud/til-notes-private", + "Private repo for the NOTES.md of my TIL repo" +] +... +``` + +Then I can add one more pipe to that `jq` query to turn it into _tab-separated +values_ using +[`@tsv`](https://jqlang.org/manual/v1.5/#format-strings-and-escaping): + +```bash +❯ gh repo list --limit 1000 --json nameWithOwner,description \ + --jq '.[] | [.nameWithOwner, .description] | @tsv' +jbranchaud/personal-site My personal site -- joshbranchaud.com +jbranchaud/til-notes-private Private repo for the NOTES.md of my TIL repo +... +``` + +This is useful because I can then pipe it to another program, such as an `fzf` +command like [this repo selector that opens the selected one in the +browser](https://github.com/jbranchaud/dotfiles/commit/f964ca10c6c4db3475411c2991dc2f1dfd18c818).