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"]
?
How can I get unique values in array in a jmespath query?
Asked Answered
Unfortunately this is not currently possible in jmespath.
I 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. –
Lepanto
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
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. –
Caffeine 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).
© 2022 - 2024 — McMap. All rights reserved.