Search code examples
jsoncsvconnectorwso2-micro-integrator

I am getting an error while converting JSON to CSV using csvModule in WSO2 MI 410


I am trying to convert JSON file to CSV file. I tried with Data Mapper, it is working fine and then I came to know new connector csvModule in MI. While converting I received below error.

"ERROR {SimpleMessageContext} - Error converting data : not a valid Json Array"

I added array in my json payload and then that error is not coming & payload properly converted, I think it's not a proper approach.

Can someone please help me with alternative approach to resolve this issue?

To resolve the error, temporarily I added array in my payload.

I am expecting JSON to CSV function of csvModule convert my payload properly.

Incoming JSON { "Name": "ABC", "Age":25 }

Expected CSV Name,Age ABC,25


Solution

  • Seems like this is a limitation of the CSV Module in the MI. If you are planning to use the CSV Module, the only option is to warp the JSON object with the [] brackets to convert it to an array.

    As a workaround, you can use a Payload factory to convert the object to an array, leaving the input payload as it is.

    <payloadFactory media-type="json">
        <format>[$1]</format>
        <args>
            <arg expression="$" evaluator="json"/>
        </args>
    </payloadFactory>
    <CSV.jsonToCsv/>