Search code examples
muledataweavemulesoft

Dataweave, How to remove the extra \n when generating a csv?


I am generating a csv file to be sent to a SFTP location. The file is created as expected except by the extra \n is there anyway to remove it?

Input:

[
    {
        "reference": "TPL",
        "newId": "07223455",
        "dateCreated": "020200509",
        "error": "000"
    },
    {
        "reference": "TIN",
        "newId": "07123455",
        "dateCreated": "020200509",
        "error": "000"
    },
    {
        "reference": "THW",
        "newId": "05550910",
        "dateCreated": "020200509",
        "error": "000"
    },
    {
        "reference": "THW",
        "newId": "05533112",
        "dateCreated": "020200509",
        "error": "000"
    },
    {
        "reference": "TSB",
        "newId": "05567865",
        "dateCreated": "020200509",
        "error": "000"
    }
]

Dataweave:

%dw 2.0
output application/csv header = false
import * from dw::core::Strings
---
payload filter ( $.error == '000' ) map (
    values:
       rightPad($.reference, 16)
    ++ rightPad('0', 1)
    ++ rightPad($.newId, 9)
    ++ rightPad('0', 1)
    ++ rightPad($.dateCreated,9)
    ++ rightPad("", 16)
    ++ rightPad("P",1)
    ++ rightPad("", 103)
 ) 

Result:

TPL             07223455  020200509                 P                                                                                                       
TIN             07123455  020200509                 P                                                                                                       
THW             05550910  020200509                 P                                                                                                       
THW             05533112  020200509                 P                                                                                                       
TSB             05567865  020200709                 P                                                                                                       
TSB             05533112  020200509                 P                                                                                                       

I tried remove the last line

result[0 to sizeOf(result) -1]

but the \n is added at the end of each line by default


Solution

  • You may want to try this:

    %dw 2.0
    output application/flatfile schemaPath="schema.ffd"
    var data = [
        {
            "reference": "TPL",
            "newId": "07223455",
            "dateCreated": "020200509",
            "error": "000"
        },
        {
            "reference": "TIN",
            "newId": "07123455",
            "dateCreated": "020200509",
            "error": "000"
        },
        {
            "reference": "THW",
            "newId": "05550910",
            "dateCreated": "020200509",
            "error": "000"
        },
        {
            "reference": "THW",
            "newId": "05533112",
            "dateCreated": "020200509",
            "error": "000"
        },
        {
            "reference": "TSB",
            "newId": "05567865",
            "dateCreated": "020200509",
            "error": "000"
        }
    ]
    ---
    data map {($ - "error"), p: "P"}
    

    You have to create a file under your classpath (I created it under src/main/resources) containing the schema, I called it schema.ffd and you can see its contents below:

    form: FIXEDWIDTH
    name: my-flat-file
    values:
    - { name: 'reference', type: String, length: 16 }
    - { name: 'newId', type: String, length: 10 }
    - { name: 'dateCreated', type: String, length: 25 }
    - { name: 'p', type: String, length: 104 }
    

    I get the following as an output:

    TPL             07223455  020200509                P                                                                                                       
    TIN             07123455  020200509                P                                                                                                       
    THW             05550910  020200509                P                                                                                                       
    THW             05533112  020200509                P                                                                                                       
    TSB             05567865  020200509                P