At some point, you may have a service that returns a lot of fields and related objects. What if the service consumer doesn’t want all the fields all the time? In other words, he/she would like to have the response filtered. Here comes the** Content Filter** to the rescue. After you read this article you will find some ideas about how to use it in your own scenarios. So let’s roll.
This is the opposite pattern to Enrchier Pattern – more about it you can find here. In this particular scenario, we want to simplify our response. Remove unnecessary information that the receiver doesn’t need.
Content Filter pattern
As you can see in the diagram above, before filter we have a message with 3 elements and after the filter is applied we have only one element. The magic is happening in the black boxes called Content Filter. However, we as designers should know how this particular element works. So, how to define filter?
We may choose two ways of doing this:
In order to filter out some fields, we have DataWeave language. So for filtering purposes, we will use the Transform Message component. We can filter properties from objects and arrays. So let see how to do the positive and negative filtering in DW.
In DataWeave we have two operators – and –. The first one allows for removing a key-value pair by providing the key.
_Remove the __title _property
Okay, that way I can remove one field at a time. In order to remove two fields like in the example above I would need to do it in the following way:
JavaScript
1
%dw 2.0
2
output application/json
3
---
4
{
5
"id": "idy4234-2",
6
"title": "Meetup"
7
} - "id" - "title"
The double dash operator (–) allows providing keys array. In other words, each key present in the array will be removed from the supplied object.
Remove both _title _ and id properties
In the example above we have provided an array with title and id keys. As a result of applying — operator, we got the empty object.
#integration #dataweave #integration pattern #content filter #negative filtering #rest