Range selector
Exercise
Section titled “Exercise”Given the following input payload:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Write a DataWeave transformation that produces the following output:
[ 5, 4, 3, 2, 1]- To get this result you have to use the range selector
[<n> to <n2>], wherenandn2are indexes. - Remember that to reverse the list you can use negative indexes.
Show solution
%dw 2.0output application/json---payload[0 to 4][-1 to 0]// o payload[-6 to 0]Notes:
- The first thing we might think of is getting the range we want first, in this case from 1 to 5.
For that we use the range selector to get those values by their indexes
[0 to 4]. Once we have the range we were after, we simply flip the order of the list using negative indexes[-1 to 0] - Another solution would be to build the range directly with a negative index, looking for the index where the 5 sits. In this case
[-6 to 0]
{Code mat;}
Learn MuleSoft: courses, exercises and quizzes. Free, no sign-up.
© 2026 Codebymat