Shreejit Malshikhare
Shreejit Malshikhare

Reputation: 25

How to filter 2 CSV files in Mule?

I have used the 'ParseTemplate' component in the Anypoint studio to get data from a .csv file. I have used two .csv files in this scenario. Like this: my flows

I want to filter these 2 payloads where the content which is not in 1st file and but which is in the second file should be displayed as a final JSON response.

Like 1st CSV file has 10 fields having id from 1-10, the second file consists of the fields present in 1st CSV file + 5 more fields having id's 11-15.

I want to display these 5 unique fields only, which is not present in the 1st file. How to achieve this?

Upvotes: 0

Views: 777

Answers (1)

user3078986
user3078986

Reputation:

It is a lot easier if you provide input data and the expected output. Moreover, I am not sure whether you want DW 1.0 or 2.0 code. So here's the DW 2.0 code:

%dw 2.0
output application/csv
// Get the headers to remove--I assume the 1st file is in the payload
var hs2rm = payload[0] pluck $$
// Read the second CSV--not sure how you do it since I do not see the details
// of parseTemplate.
var csv = readUrl("classpath://other.csv","application/csv")
---
// Iterate over the records of the second CSV file
csv map (
    // Remove the headers of the 2nd CSV that match the headers of the 1st
    $ -- hs2rm
)

Here's the DW 1.0 expression to do the same as above:

%dw 1.0
%output application/csv

// Get the headers to remove--I assume the 1st file is in the payload
%var fs2rm = payload[0] pluck $$
// Read the second CSV
%var csv = readUrl("classpath://other.csv","application/csv")
// Define a function to iterate over the object and remove fields
// passed in a list of :key types (i.e. not :string)
%function rmFields(o,fs) 
    o mapObject (
        {}  when (fs contains $$)
            otherwise {($$):$}
    )
---
// Iterate over the records of the second CSV file
csv map (
    // Remove the headers of the 2nd CSV that match the headers of the 1st
    // There is no convenient way to do it in DW 1.0 as in DW 2.0
    rmFields($, fs2rm)
)

Now that you have provided the sample for the inputs and the expected output I know what you are looking for--because the original description at the top is at the very least not clear--and I can provide you with the code. Here it is:

%dw 1.0
%output application/json
%var firstcsv = flowVars.set_1stpayload
%var secondcsv = flowVars.set_2ndpayload
---
secondcsv -- firstcsv

Upvotes: 3

Related Questions