2

Facing issues while converting CSV to XML using dataweave 2.0 in mule 4.

input payload (CSV):

employee_id,amount  
12345,75  
67890,15  
13579,38  

Output Result (XML):

<ch:Data xmlns:ch="xxx:com.abcdef.report">
  <ch:Entry>
     <ch:Employee_ID>12345</ch:Employee_ID>
     <ch:Cost>75</ch:Cost>
  </ch:Entry>
  <ch:Entry>
     <ch:Employee_ID>67890</ch:Employee_ID>
     <ch:Cost>15</ch:Cost>
  </ch:Entry>
  <ch:Entry>
     <ch:Employee_ID>13579</ch:Employee_ID>
     <ch:Cost>38</ch:Cost>
  </ch:Entry>
</ch:Data>
VinnyChinn
  • 39
  • 2
  • 8

1 Answers1

4

You need to use the dynamic object feature so that it expands the array in the parent object

%dw 2.0
ns ch xxx:com.abcdef.report
output application/xml
---
ch#Data: {
    (payload map {
        ch#Entry: {
            ch#Employee_ID: $.employee_id,
            ch#Cost: $.amount 
        }
    })
}
machaval
  • 4,969
  • 14
  • 20
  • Am getting below error while using this code "\"You called the function 'map' with these arguments: \n 1: Binary (\"employee_id,amount\\r\\n12345,75\\r\\n56432,15\\r\\n67890,5\\r\\n\" as Binary {encodi...)\n 2: Function (($:Any, $$:Any) -> ???)\n\nBut it expects arguments of these types:\n 1: Array\n 2: Function\n\n\n 6| \t(payload map {\n | ...\n11| \t})\n\nTrace:\n at map (line: 6, column: 3)\n at main (line: 6, column: 11), while writing Xml at \n 6| \t(payload map {\n | ...\n11| \t})\n.\n\n 6| \t(payload map {\n | ...\n11| \t})\n\nTrace:\n at main (line: 6, column: 3)\" – VinnyChinn Aug 07 '19 at 19:22
  • This is because your payload doesn't have the mimeType set. Where are you getting your payload from? – machaval Aug 07 '19 at 19:47
  • Changing MIME type to application/csv for input payload worked. Thank you @Machaval – VinnyChinn Aug 07 '19 at 20:16
  • I'm glad to help – machaval Aug 07 '19 at 20:53