How to pass the dynamic fieldName in filter condition of dataweave mule

2.5k views Asked by At

I have data in cache i.e objectstore. I need to get the data from cache and apply the filter condition on it.

Here requirement is, I have to expose api to get the data based on filedName and filedValue which will come dynamically in the request.

sample request :

https://localhost:8082/api/customer/filter?fieldName=customerId&fieldValue=101810

I have return code to filter the data in dataweave but it is not working. can you please help on this

%dw 1.0
%output application/json
---
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
    customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
    customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
    customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})

and I am getting below error

Exception while executing: 
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
                      ^
Type mismatch for '++' operator
     found :object, :string
  required :string, :string.

can you please help on this

1

There are 1 answers

2
AnupamBhusari On BEST ANSWER

The issue is with the code used for selector it should be like $.content[flowVars.fieldName].content. Complete code will be

%dw 1.0
%output application/json
---
payload.rows filter (($.content[flowVars.fieldName].content) as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
    customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
    customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
    customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})

This worked fine with the input provided by you.

Hope this help.