Use pluck to transform Objects into Arrays

Use pluck to transform Objects into Arrays

Why pluck?

In DataWeave 2.0 (Mule 4.2), you can’t use the recursive update operator, but you still have powerful tools for reshaping data. The pluck operator lets you iterate over an Object’s key – value pairs and return a clean Array – perfect for further array-based processing (filter, map, slice, etc.).

Syntax

		
		
	

  • value: the field’s value
  • key: the field’s name
  • index: position in the resulting Array
  • <expression>: any DataWeave expression that returns one element of the new Array

Example

Suppose you have sales counts per product:

		
		
	

To turn this into an Array of { product, count } objects:

		
		
	

Result:

		
		
	

Benefits

  • Readable: No nested loops or manual mapObject logic.
  • Flexible: Easily include or modify fields in the output expression.
  • Composable: Once in Array form, you can apply any Array operators (filter, map, take, etc.).

Conclusion

When you need to break an Object into an Array of custom records in DataWeave 2.0, reach for pluck. It keeps your code concise, clear, and fully leverages the Array transformations that follow.