Ask Your Question
2

Using JMESPath, how many occurrences are there in an array?

asked 2023-06-05 11:10:58 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-05 11:15:02 +0000

lalupa gravatar image

To count the number of occurrences in an array using JMESPath, you can use the length() function. For example, if you have an array of numbers like this:

{
  "numbers": [1, 2, 3, 2, 4, 2, 5]
}

You can count how many times the number 2 appears in the array by using the following JMESPath expression:

length(numbers[?@ == 2])

This will return the value 3, which corresponds to the number of times 2 appears in the numbers array.

In this expression, numbers[?@ == 2] is a filter that selects only the elements of the array that match the value 2. The length() function then counts the number of items in this filtered array, which gives the number of occurrences of 2 in the original array.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-05 11:10:58 +0000

Seen: 8 times

Last updated: Jun 05 '23