Unix 1-liner using jq
Recently, I needed to get all tokens for accounts that were PENDING_REVIEW in a system, here’s how to do it in a unix 1 liner.
./get-all-accounts | jq .data | map(select(.status == "PENDING_REVIEW")) | map(.token) | .[] -r | pbcopy
First, write a small script to get all accounts and output as json. In this case it’s a curl, but if it was a paginating API, we’d want to handle that inside the first step. We’ll call it get-all-accounts.
The JSON response is then piped into jq, a command-line JSON processor.
The command is broken down into four transformations:
.data: This takes the “data” property of the root object.
Output after this stage looks like this:
[
{
"created": "2023-08-09T13:21:17.112064",
"email": "balancing+kyc+readonly@paymentcorp.com",
"status": "APPROVED",
"token": "7db20fa2-cb36-403f-d551-1585730b42f8"
},
...
]
map(select(.stat…