The pluck() function in DataWeave 2 is a function used to convert the properties and values of these properties of an object into an array of properties, an array of values and an array of indices of those properties.
The syntax of the pluck() function will be:
1 |
<input> pluck (value, key, index) -> <...> |
In particular, index is the order of the properties of the input object, the key is the name of the object’s properties and the value is the value of the properties.
For example, I have a student object with the name attribute. has the value “Khanh” and the age attribute has a value of 30, displayed with mediaType application/xml with the following content:
1 2 3 4 |
<student> <name>Khanh</name> <age>30</age> </student> |
To return the array of student object properties, I will use the pluck() function as follows:
1 2 3 4 5 6 7 8 9 10 11 |
%dw 2.0 output application/xml var data = read("<student> <name>Khanh</name> <age>30</age> </student>", "application/xml") --- result: { key: data.student pluck (value, key, index) -> key } |
Result:
Same for index and value:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
%dw 2.0 output application/xml var data = read("<student> <name>Khanh</name> <age>30</age> </student>", "application/xml") --- result: { index: data.student pluck (value, key, index) -> index, key: data.student pluck (value, key, index) -> key, value: data.student pluck (value, key, index) -> value } |
Result:
You can use the characters $, $$ or $$$ instead of value, key and index in order.
1 |
<input> pluck ($) |
for value,
1 |
<input> pluck ($$) |
for key and:
1 |
<input> pluck ($$$) |
for index.
As a result, the following writing way still produces similar results:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
%dw 2.0 output application/xml var data = read("<student> <name>Khanh</name> <age>30</age> </student>", "application/xml") --- result: { index: data.student pluck ($$$), key: data.student pluck ($$), value: data.student pluck ($) } |
Result:
With this writing, as you can see, we do not need to specify the value, key or index parameters. DataWeave will understand itself and produce similar results.