Selecting data with filter & filterObject

Selecting data with filter & filterObject

When you need to pick and choose which entries to include, DataWeave’s filter and filterObject helpers let you prune arrays or objects based on any condition.

When to Use

  • filter evaluates each element of an array against a predicate and returns a new array of elements that pass the test.
  • filterObject evaluates each key–value pair of an object and returns a new object containing only those entries for which the predicate is true.

filter in Action

Use filter to discard array elements that don’t meet your criteria. The predicate receives (value, index) and returns true to keep the element or false to drop it.

Example: Extract only even numbers from a list:

		
		
	

Here, filter builds a new array containing only the elements for which n mod 2 == 0 is true.

filterObject in Action

Use filterObject when you want to remove or keep object entries dynamically. The predicate receives (value, key, index) and returns a boolean.

Example: Remove properties whose values are null or empty strings:

		
		
	

This keeps your payload free of empty or irrelevant fields.

Best Practices for Selectors

  • Combine with default to handle missing data:
  • 		
    		
    	

  • Keep predicates simple for readability and performance.
  • Document intent with comments when predicates are complex.

Conclusion

By applying filter to arrays and filterObject to objects, you can prune your data precisely and efficiently. Keep predicates clear, leverage defaults for missing values, and chain with transformers for end-to-end pipelines. With these selectors in your toolkit, you’ll produce payloads that contain only the data you need—clean, lean, and ready for the next step.