简体   繁体   中英

How can I get unique values in array in a jmespath query?

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"] ?

不幸的是,这在 jmespath 中目前是不可能的。

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM