简体   繁体   中英

CSV TO JSON using dataweave

Please refer this dataweave window image :- dataweave window image

I am trying to transform a CSV TO JSON using dataweave but even for the simplest of transformations it is creating a null tag automatically. I can see that in the preview window. When I run this application I am getting this error *

Exception stack is: 1. 452 (java.lang.ArrayIndexOutOfBoundsException) com.mulesoft.weave.reader.CharArraySourceReader:21 (null) 2. 452 (java.lang.ArrayIndexOutOfBoundsException). Message payload is of type: WeaveMessageProcessor$WeaveOutputHandler (org.mule.api.MessagingException)
org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32

******************************************************************************** Root Exception stack trace: java.lang.ArrayIndexOutOfBoundsException: 452 at com.mulesoft.weave.reader.CharArraySourceReader.lookAheadAscii(CharArraySourceReader.scala:21) at com.mulesoft.weave.reader.csv.parser.CSVParser.parse(CSVParser.scala:132) at com.mulesoft.weave.reader.csv.parser.CSVParser.elementAt(CSVParser.scala:61) at com.mulesoft.weave.reader.csv.parser.CSVParser.contains(CSVParser.scala:38) at com.mulesoft.weave.reader.csv.CSVRecordsValue$$anon$1.hasNext(CSVReader.scala:58) at scala.collection.Iterator$class.toStream(Iterator.scala:1188) at com.mulesoft.weave.reader.csv.CSVRecordsValue$$anon$1.toStream(CSVReader.scala:56)

This is the sample csv I am using

SpreadsheetKeyEmployee,Position,EffectiveDate,BonusPlan,Amount,Currency,IgnorePlanAssignment
1,18211,2016-05-01,BONUS_PLAN1,150,USD
2,18212,2016-05-01,BONUS_PLAN2,150,USD
3,18213,2016-05-01,BONUS_PLAN3,150,USD
4,18214,2016-05-01,BONUS_PLAN4,150,USD

I think i might be doing some mistake with the reader configuratuion (for csv) there is a similar issue discussed in the forum but this isnt helping either https://forums.mulesoft.com/questions/36378/dataweave-example-of-csv-to-json.html

I have tried recreating your scenario but unable to replicate it, here is the dataweave configuration that I have used and it works.

<dw:transform-message metadata:id="e4e1b720-5d25-4b36-8406-cf7d6bfc7d6a" doc:name="CSV to JSON">
        <dw:set-payload><![CDATA[%dw 1.0
         %output application/json
         ---
         payload map ((payload01 , indexOfPayload01) -> {
              SpreadsheetKeyEmployee: payload01.SpreadsheetKeyEmployee as :number,
         Position: payload01.Position,
         EffectiveDate: payload01.EffectiveDate,
         BonusPlan: payload01.BonusPlan,
         Amount: payload01.Amount,
         Currency: payload01.Currency,
        IgnorePlanAssignment: payload01.IgnorePlanAssignment
        })]]></dw:set-payload>
  </dw:transform-message>

It might be the case that dataweave cannot handle window line endings correctly. Can you try to replace the windows new line delimiter with \\n only as follows:

#[payload.replace("\r\n", "\n")]

You can place the above MEL expression in a set-payload before dataweave.

You need to parse the csv file and map it to JSON object in dataweave component. Check the link below

https://dzone.com/articles/csv-xml-json-in-mulesoft

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM