DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Davide de Paolis
Davide de Paolis

Posted on

Quick Tip to write Cognito Emails to File using JQ and Pipe

Last week it i needed somehow to retrieve all the emails of the users in the Cognitor User-Pool of an app we use internally in our company.
Cognito User Pool
They were not a lot and I probably could have spent some good 30 minutes going through all the users listed in the AWS UI-Console and then copy-pasting them into an excel file.
But I hate doing things manually, especially if boring and repetitive tasks, therefore, I quickly opened the AWS Cognito Documentation and JQ tutorials to figure out how to quickly grab the list of emails:
This is the nifty little line of code that saved me from becoming a copy-pasta-monkey:

aws cognito-idp list-users --user-pool-id YOUR_USER_POOL | jq '[.Users[].Attributes[].Value | match(".*@.*") | .string]' > emails.json 
Enter fullscreen mode Exit fullscreen mode

Yes, maybe it took me still around 20 minutes to figure it out but by doing that I had the opportunity of learning something new about the AWS Cognito API, of refreshing my JQ parsing skills, and i have a tiny snippets anyone in my team could use whenever a similar task may arise.

Tadaa!


What is it doing?

aws cognito-idp list-users --user-pool-id YOUR_USER_POOL is just asking the AWS API to retrieve the users from a specific Cognito User Pool
| the Pipe feeds the result of the API call as input parameter of JQ command
.Users[].Attributes[].Value grabs all the values of the properties of each users nested in the API Response
| feeds the result into the next regex
match(".*@.*") which searches for the email values
| feeds the list of emails found by the regex to the last JQ command
.string which extracts the string value out of the RegEx match.
The list of emails is nicely wrapped into an array ( see the brackets at start and end of the entire JQ command
> emails.json writes the result of the piped commands into a file. ( > is creating always a new file - while >> will append to the content , if existing)

Other tips and tools here

Top comments (0)

πŸ‘€ Just want to lurk?

Β 
That's fine, you can still create an account and turn on features like 🌚 dark mode.