-1

Input [ { "storeNumber": 1, "storeDescription": "Test", "storeBrand": "John", "brandCode": 101, "dieselBrand": "Brand1", "gasolineBrand": "Brand2", "isDealer": false }, { "storeNumber": 2, "storeDescription": "Test", "storeBrand": "John", "brandCode": 102, "dieselBrand": "Brand1", "gasolineBrand": "Brand2", "isDealer": false }, { "storeNumber": 33, "storeDescription": "Test", "storeBrand": "John", "brandCode": 101, "dieselBrand": "Brand1", "gasolineBrand": "Brand2", "isDealer": true }, { "store number": 666, "store description": null, "store-brand": null, "brand code": null, "dieselBrand": null, "gasolineBrand": null, "isDealer": false } ] And here is the DW code

output application/json var brandCode = 262 var isDealer = null

payload filter

// FILTER ONLY BY BRAND CODE

(((brandCode != null) and (brandCode contains $.brandCode) and (isDealer == null))

// FILTER BY BRAND CODE AND IS DEALER

or ((brandCode != null) and (brandCode contains $.brandCode) and ($.isDealer == isDealer))

// FILTER ONLY BY IS DEALER

or ((brandCode == null) and ($.isDealer == isDealer))

// NO FILTER

or ((brandCode == null) and (isDealer == null)))

PROBLEM: If I want to add 2 more filter criteria it will be very complicated and I would like to know if someone knows a simple solution for this filtering.

I need to filter the payload based on locationId, statusCode and status. I need to be able to filter after multiple values, for example locationId: 1,2 statusCode: 102 class: "Close" and also if I don't have any value the filter will be ignored.

obc
  • 7
  • 3
  • Does this answer your question? [MuleSoft JSON Array Filter](https://stackoverflow.com/questions/57366264/mulesoft-json-array-filter) – Harshank Bansal Oct 15 '22 at 08:57

1 Answers1

0

When you said "if I don't have any value the filter will be ignore.", it is not entirely clear to me what "any value" refers to.

If you prefer a particular filter criteria to be ignored because there is no value currently set to filter for it, then the following DataWeave script will retrieve only the objects in the payload which have property values that match one of the values assigned to the property in the configured filterSet variable:

var filterSet = {
    "locationId": [1,2],
    "statusCode": [102],
    "status": ["Open"]
}
---
filter(payload, (v0, k0) -> (
    entriesOf(filterSet) reduce (entry, acc = true) -> acc and 
      if (!isEmpty(entry.value default [])) (entry.value contains v0[entry.key]) else true
))

Otherwise, if you prefer a particular filter criteria to be ignored when an object in the payload does not have a value for the property being filtered, then this next version of DataWeave script will retrieve only the objects in the payload which either have a missing property value or a property value which matches one of the values assigned to the property in the configured filterSet variable:

var filterSet = {
    "locationId": [1,2],
    "statusCode": [102],
    "status": ["Open"]
}
---
filter(payload, (v0, k0) -> (
    entriesOf(filterSet) reduce (entry, acc = true) -> acc and 
      (isEmpty(v0[entry.key]) or (entry.value default [] contains v0[entry.key]))
))
ToneDeF
  • 36
  • 3