Home > Software engineering >  Write to CSV obejct to array Mulesoft
Write to CSV obejct to array Mulesoft

Time:03-22

I have this output and I'm trying change data format for YYYY-MM-DD and I would like write this object to CSV using component File WRITE.

%dw 2.0
output application/json
---
{
    "id": "15",
    "key": "DEMO-123",
    "first": {
        "date": "2022-02-08T14:40:35.935 0100" as
         DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.sssxx"} as String {format: "yyyy-MM-dd"},
        "demotype": {
            "firstname": "Task",
            "opinion": "demo input"
        },
        "demo": {
            "name": "Tree"
        },
        "start": "2022-02-08T07:23:53.054 0100",
        "end": "2022-02-08T14:40:35.935 0100",
        "status": {
            "name": "good"
        }
       "demo2": [
            {
                "kiss": [
                    "demo1"
                ]
            }
}

I was trying do the map something like this:

<ee:transform doc:name="Transform Message">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/csv quoteValues=true, separator=";", header=true

---
flatten (
    [
        "id": "id",
        "key": "key"
    ]
)]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <file:write doc:name="Write" config-ref="File_Config" path="F:user/demo.csv"/>

But I got In CSV only headers id and key without value. But If I trying using map there is the error that I need transform this object to array to write it to CSV.

CodePudding user response:

Your first script that generates the 'input' for the transformation to CSV is not valid and the date time pattern is not valid. I fixed it. I also set the output to application/java to be more efficient in the next transformation:

%dw 2.0
output application/java
---
{
    "id": "15",
    "key": "DEMO-123",
    "first": {
        "date": "2022-02-08T14:40:35.935 0100" as
         DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSxx"} as String {format: "yyyy-MM-dd"},
        "demotype": {
            "firstname": "Task",
            "opinion": "demo input"
        },
        "demo": {
            "name": "Tree"
        },
        "start": "2022-02-08T07:23:53.054 0100",
        "end": "2022-02-08T14:40:35.935 0100",
        "status": {
            "name": "good"
        },
       "demo2": [
            {
                "kiss": [
                    "demo1"
                ]
            }
       ]
    }
}

To generate a CSV output the input has to be an array of object, where each object represents a record, or row of the CSV. Since you input is apparently a single object, you could just enclose the transformation between array brackets to output a single item array. You can use the following example as the basis to complete your desired transformation. Note that demo2 contains an array, which contains an object with another array. You can't just output that to a CSV field. If you want to do that first transform it into a string. For example you can use the reduce() function if needed.

%dw 2.0
output application/csv quoteValues=true, separator=";", header=true
---
[
    {
        id: payload.id,
        key: payload.key,
        date: payload.first.date,
        start: payload.first.start
    }
]

Output:

id;key;date;start
"15";"DEMO-123";"2022-02-08";"2022-02-08T07:23:53.054 0100"

Note that flatten() doesn't do anything in a simple array. If you tried to use it to 'flat' a nested object/array be aware that it doesn't do that.

  • Related