The AWS CLI can render results as JSON, a table, or plain text, and --query filters them with JMESPath before they print. This makes the CLI a fast companion to your SDK code for checks and scripting.
The same "list running instance IDs" done with the CLI, then in both SDKs.
# CLI: text output is script-friendly (no quotes, tab-separated)aws ec2 describe-instances \ --filters "Name=instance-state-name,Values=running" \ --query "Reservations[].Instances[].InstanceId" \ --output text
# First element--query "Reservations[0].Instances[0].InstanceId"# Filter by a field, then project--query "Reservations[].Instances[?State.Name=='running'].InstanceId"# Rename fields into columns--query "Buckets[].{Name:Name,Created:CreationDate}"# Sort and take a slice--query "sort_by(Volumes, &Size)[-3:].VolumeId"
--query does not reduce data transfer - It runs after the whole response arrives, so it is not a substitute for server-side --filters on huge lists. Fix: use --filters to limit results, then --query to shape them.
Quoting on Windows - Single quotes inside --query break in cmd.exe. Fix: use double quotes outside and escape inner quotes, or run in PowerShell/bash.
text output and empty results - An empty --query result prints nothing, which can look like an error. Fix: check the exit code, not just stdout.
Assuming v3 has JMESPath - The AWS SDK for JavaScript v3 has no query helper. Fix: filter with map/filter/flatMap in TypeScript.
Pagination cut-off - A raw describe-* call returns only one page, so --query sees partial data. Fix: the CLI auto-paginates by default, but in SDK code use a paginator.
None vs missing keys - JMESPath returns null for missing fields, which prints as None/blank in text. Fix: guard for nulls before using values downstream.