Index Selector ([anIndex])
The index selector returns the element at the specified position. It can be applied over an array, object, or string.
Now that we understand how to traverse Objects with the single-value selector, let’s see how to traverse Arrays with the index selector ([n]). Use the index selector to get to a value in an Array based on its position from the beginning of the Array:
Input:
[“prod”, “qa”, “dev”]
DW Script:
%dw 2.0
output json
—
payload[1]
Output:
“qa”
Notice that by using 1 as the index, the script returns the second item in the Array. This is because Arrays in DataWeave are zero-indexed; the item in the first position of the Array has an index of 0, the second has an index of 1, and so on.
Just like Objects can be nested, so can Arrays, and you can retrieve nested Array items in the same way you do with the single-value selector, by stringing together index selectors.
There’s another important feature that should be noted. If you use positive numbers for the index, DataWeave will start selecting from the beginning of the Array, but if you use a negative number for the index, DataWeave will start selecting from the end of the Array. Since 0 is already reserved as the first element in the Array, and there is no such thing as -0, DataWeave starts indexing the last item of the Array from -1.
Index Selector Over an Array
This selector can be applied to String literals, Arrays and Objects. In the case of Objects, the value of the key-value pair found at the index is returned. In the case of Arrays, the value of the element is returned. The index is zero-based.
- If the index is bigger or equal to 0, it starts counting from the beginning.
2. If the index is negative, it starts counting from the end where -1 is the last element.
DataWeave Script:
%dw 2.0
output application/json
—
payload.people[1]
Input JSON Payload:
{
“people”: [
{
“nameFirst”: “Nial”,
“nameLast”: “Martinez”
},
{
“nameFirst”: “Coty”,
“nameLast”: “Belgrano”
}
]
}
Output JSON:
{
“nameFirst”: “Coty”,
“nameLast”: “Belgrano”
}
Index Selector Over an Object
The selector returns the value of the key-value pair at the specified position.
DataWeave Script:
%dw 2.0
output application/json
—
payload[1]
Input JSON Payload:
{
“nameFirst”: “Mark”,
“nameLast”: “Nguyen”
}
Output JSON:
“Nguyen”
Index Selector Over a String
When using the Index Selector with a string, the string is broken down into an array, where each character is an index.
DataWeave Script:
%dw 2.0
output application/json
—
{ name: “MuleSoft”[0] }
Output JSON:
{ “name”: “M” }
The selector picks the character at a given position, treating the string as an array of characters.
- If the index is bigger or equal to 0, it starts counting from the beginning.
- If the index is negative, it starts counting from the end.
DataWeave Script:
%dw 2.0
output application/json
—
{ name: “Emiliano”[0] }
Output JSON:
{ “name”: “E” }
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
Leave a Comment