Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.