Search code examples
csvtrimspacesmulesoft

How to remove extra spaces in CSV header


In Mulesoft (version 4.2.1), I'm getting CSV file as input and in headers, I'm getting some spaces along with header name, which I'm not expecting. For example, I'm getting, 'FirstName ' instead of 'FirstName'. How to remove extra spaces from header, before processing.


Solution

  • This is one way to do it.

    %dw 2.0
    output application/json
    import * from dw::core::Objects
    var headers=keySet(payload[0]) reduce ((key, acc = {}) -> acc ++ { (trim(key)): key})
    fun searchKeyTrimmed(row, h)=row[headers[h]]
    ---
    payload map {
        a: searchKeyTrimmed($,'a'),
        b: searchKeyTrimmed($,'b')
    }
    

    Input:

    a , b,c,d 
    1,2,3,4
    

    Output:

    [
      {
        "a": "1",
        "b": "2"
      }
    ]