In an aws cli jmespath query, with for example the output ["a","a","b","a","b"]
, how do i extract the unique values of it to get ["a","b"]
?
Asked
Active
Viewed 6,466 times
12

Frido Emans
- 5,120
- 2
- 27
- 42
3 Answers
16
Unfortunately this is not currently possible in jmespath.

Jordon Phillips
- 14,963
- 4
- 35
- 42
-
2I would argue that jmespath is grep, not gawk. It just provides a selection of data. That is it's job. Processing json is someone else's business! :-) Now what that other tool should be is another question and I think the answer depends on context. – Max Murphy Feb 07 '17 at 12:41
7
It's not what you asked for but I've used the following:
aws ... | jq -r ".[]" | sort | uniq
This will convert ["a", "a", "b", "a"]
to:
a
b

mekazu
- 2,545
- 3
- 23
- 21
-
This is a great workaround. In PowerShell on Windows, I installed `jq` with `winget install stedolan.jq`. You may need the latest winget 1.4 `winget install winget`. Then `| jq -r ".[]" | sort` worked. – Cameron Taggart Apr 06 '23 at 04:21
0
The closest I've come to "unique values"... is to deduplicate outside of JMESPath (so not really in JMESPath pipelines).
aws ec2 describe-images \
--region us-east-1 \
--filter "Name=architecture,Values=x86_64" \
--query 'Images[].ImageOwnerAlias | join(`"\n"`, @)' \
--output text \
| sort -u
Output:
amazon
aws-marketplace
If you use JMESPath standalone, you'd write things like this.
jp -u -f myjson.json 'Images[].ImageOwnerAlias | join(`"\n"`, @)' | sort -u
The idea is to get jp to spit out a list of values (on separate lines) and then apply all the power of your favorite sorter. The tricky part is to get the list (of course).

Alain Pannetier
- 9,315
- 3
- 41
- 46