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

3

14

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

Publicspirited answered 4/5, 2016 at 8:48 Comment(0)
B
17

Unfortunately this is not currently possible in jmespath.

Benedictus answered 5/5, 2016 at 17:49 Comment(1)
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
A
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
Antemortem answered 5/3, 2020 at 22:59 Comment(1)
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
H
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).

Hall answered 9/1, 2023 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.