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(.status == “PENDING_REVIEW”)): This maps over the array and selects only the objects where the “status” equals “PENDING_REVIEW”.
Output after this stage looks like this:
[
{
"created": "2022-06-07T12:13:19.727099",
"email": "security+kyc+writeonly@paymentcorp.com",
"status": "PENDING_REVIEW",
"token": "8dc20fd2-db47-405f-f591-1015852b42e7"
}
]
map(.token): This maps over the array from the previous stage and selects only the “token” property of each object.
Output after this stage looks like this:
[
"8dc20fd2-db47-405f-f591-1015852b42e7"
]
.[]: This reduces the array to a single value (assuming theres only one item in the array).
Output after this stage is just:
8dc20fd2-db47-405f-f591-1015852b42e7
Finally, it pipes this value into pbcopy command, which copies the content to your clipboard. In the end, you will have all account tokens with “PENDING_REVIEW” status copied to your clipboard.