1
votes

I would like to apply a filter on the input payload. The input is XML like:

<result>
    <person>
        <id>1</id>
        <zipcode>1111AB </zipcode>
        <housenr>1 </housenr>
    </person>
    <person>
        <id>1</id>
        <zipcode>1111AC </zipcode>
        <housenr>2</housenr>
    </person>
    <person>
        <id>1</id>
        <zipcode>1111AD </zipcode>
        <housenr>3</housenr>
    </person>
    <person>
        <id>1</id>
        <zipcode>1111AB </zipcode>
        <housenr>4</housenr>
    </person>
</result>

The end user is able to search on zipcode and optionally a housenr. The output json is like:

{
    persons: [{
        id: "1"
        address: "1111AB 1"
    },
    { 
        id: "2",
        address: "1111AC 1"
    }
    ]
}

So I had the idea to filter a map like so:

{
persons: payload.result.*person filter ($.zipcode == flowVars.zipcode) map{
id: $.id,
address: $.zipcode ++ $.housenr
}

I would like to know if it would be possible make that filter conditional. For example if flowVars Zipcode and Housnr are null then the filter should not be applied. Or if Zipcode is filled but housnr not, then only zipcode filter should be applied.

Help welcome!

thanks

2

2 Answers

0
votes

here is what your dataweave could look like:

%dw 1.0
%output application/json
---
{
    persons: payload.result.*person filter flowVars.zipcode == null or (trim $.zipcode) == flowVars.zipcode map {
        id: $.id,
        address: $.zipcode ++ " " ++ $.housenr
    }
}

note: i added trim to $.zipcode, because your sample XML contains spaces in zipcode.

0
votes
  {
  persons: using (personFilter = (payload.result.*person filter $.zipcode == flowVars.zipcode when flowVars.zipcode != null 
  otherwise payload.result.*person) )
  personFilter filter $.housenr == flowVars.housenr when flowVars.housenr != null otherwise (payload.result.*person filter $.zipcode == flowVars.zipcode when flowVars.zipcode != null 
  otherwise payload.result.*person)
   map {
  id: $.id,
  address: $.zipcode ++ $.housenr
      }  
  }