Created
March 2, 2026 05:57
-
-
Save fizz/715f63d6ad36d4b98c7749921302f770 to your computer and use it in GitHub Desktop.
extract_key — pull a single key from any JSON-producing command via jq. Generic zsh utility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env zsh | |
| # extract_key — pull a single key from a JSON-producing command | |
| # | |
| # Generic pattern: run any command that outputs JSON, extract one key via jq. | |
| # The last argument is always the key; everything before it becomes the command arguments. | |
| # | |
| # Usage: | |
| # extract_key <command> [args...] <json_key> | |
| # | |
| # Examples: | |
| # # Extract "status" from a ticket viewer | |
| # extract_key jira-status NA-393 status | |
| # | |
| # # Extract "email" from a user lookup | |
| # extract_key gh api users/octocat email | |
| # | |
| # # Works with any command that outputs a JSON object | |
| # extract_key curl -s https://api.example.com/item/42 name | |
| # | |
| # Requires: jq | |
| function extract_key() { | |
| if (( $# < 2 )); then | |
| print -u2 'Usage: extract_key <command> [args...] <key>' | |
| return 1 | |
| fi | |
| local key=$argv[-1] # last word is the key | |
| local cmd=("${(@)argv[1,-2]}") # everything else is the command | |
| "${cmd[@]}" | jq -r --arg key "$key" '.[$key]' | |
| } | |
| # If sourced, the function is available. If executed directly, run it. | |
| if [[ "${(%):-%N}" == "$0" ]]; then | |
| extract_key "$@" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment