JSONPath and JMESPath in AWS CLI
How AWS CLI uses JMESPath for --query filtering — practical examples for EC2, S3, and IAM.
Published:
Tags: JMESPath AWS CLI query, AWS CLI --query filter, EC2 JMESPath examples
JSONPath and JMESPath in AWS CLI The raw output of is hundreds of lines of JSON. With , you cut it to exactly the fields you need — in one command, no required. --- How AWS CLI uses JMESPath? AWS CLI's flag accepts JMESPath expressions. Every AWS command returns JSON (unless you specify ), and the expression is evaluated against that JSON before anything is printed. The key insight: — EC2 responses are doubly nested (a list of Reservation objects, each containing a list of Instances). Forgetting this is the most common source of empty or results. --- What is step-by-step workflow? Run the command without to see the raw shape. Find your path — use the JMESPath Tester with the raw output to build and verify the expression. Add the filter to your command. Set to (one value per line),…
Frequently Asked Questions
How do I use --query in AWS CLI?
Add --query 'your.jmespath.expression' to any aws command. The expression is evaluated against the JSON response before it's printed. For example, aws s3api list-buckets --query 'Buckets[*].Name' returns just the bucket name strings. Combine with --output text for easy scripting.
How do I filter EC2 instances with JMESPath?
Use a filter projection on the Reservations array: aws ec2 describe-instances --query 'Reservations[*].Instances[?State.Name==`running`].{ID:InstanceId,Name:Tags[?Key==`Name`]|[0].Value}'. This returns only running instances with their ID and Name tag value.
How do I get a list of S3 bucket names with AWS CLI?
Run: aws s3api list-buckets --query 'Buckets[*].Name' --output text. The --output text flag prints one name per line, making the result easy to pipe to xargs or other shell commands.
What is the difference between aws cli --query and jq?
Both filter JSON output, but --query uses JMESPath which runs inside the AWS CLI before any text is printed. jq is a separate tool that post-processes the raw text output. --query is more portable (no jq install required) and integrates with --output table and --output text. jq is more powerful for complex transformations.
How do I format AWS CLI output as a table?
Add --output table to any command with a --query that returns a list of objects. For example: aws ec2 describe-instances --query 'Reservations[*].Instances[*].{ID:InstanceId,State:State.Name}' --output table produces a formatted ASCII table.
All articles · theproductguy.in