1

I have an array of users and each user has couple of roles. The example of the array is shown below.

[
    {
        id: 1,
        username: 'john',
        roles: [
            {id: 500, name: 'Admin'},
            {id: 501, name: 'Owner'}
        ]
    },
    {
        id: 2,
        username: 'joe',
        roles: [
            {id: 500, name: 'Admin'},
        ]
    },
    {
        id: 3,
        username: 'june',
        roles: [
            {id: 502, name: 'User'},
        ]
    }
]

I'm trying to get all the users who have an Admin role by using jmesPath. I've tried [?roles[].name=='Admin'] but this is giving a null value. Is is possible to do this with jmesPath, and if yes can you give me an expression for this example?

2 Answers 2

4

I played a little with this example and your data

http://jmespath.org/examples.html#filtering-and-selecting-nested-data

I made it work with this expression

[?roles[?name=='Admin']]
Sign up to request clarification or add additional context in comments.

1 Comment

This actually works, but I can't understand your query. Can you please explain it here?
-1

You can get the same result with plain JavaScript using:

Assuming your array is stored on var users = [...].

users.filter((i)=> i.roles.some((s) => s.name === "Admin"))

You can see this sample with jsBin

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.