Reputation: 709
I have a person.xml,
<?xml version="1.0" encoding="UTF-8"?>
<person>
<firstName>First Name</firstName>
<lastName>Last Name</lastName>
</person
I have person.xsd,
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I need to convert it to person.csv,
firstName,lastName
First Name,Last Name
I am using Anypoint Studio 7.3 on windows 10, I am using a Listener, Read (file) xml.
See Transform Message dw below,
%dw 2.0
output application/csv separator=",", header=true
---
payload
I am getting the following message on executing the flow,
"Cannot coerce Object { encoding: UTF-8, mediaType: text/xml; charset=UTF-8, mimeType: text/xml, raw: org.mule.weave.v2.el.SeekableCursorStream@2fdc554d } ({person: {firstName: "Shahjahan",lastName: "Ravjee"}} as Object {encoding: "U...) to Array, while writing CSV at payload." evaluating expression: "%dw 2.0
output application/csv separator=",", header=true
---
payload
".
Upvotes: 0
Views: 2642
Reputation: 709
The following dw seems to have worked like a charm,
%dw 2.0
output application/csv headerLineNumber = 0 , header = true
---
[{
firstName: payload.person.firstName,
lastName: payload.person.lastName
}
]
Upvotes: 0
Reputation: 2233
The problem is, as the error message is stating, you're trying to convert an object into an array. This is a problem because in order to make a CSV, you need an array of arrays, or an array of objects.
You can see if you're currently outputting an object or array by setting your output to application/dw
.
%dw 2.0
output application/dw
---
payload
This will give you the following, which as you can see is an object:
{
person: {
firstName: "First Name",
lastName: "Last Name"
}
}
CSV needs an array, so we'll need to do a little transformation to get it to this:
[
{
firstName: "First Name",
lastName: "Last Name"
}
]
If you set the following script to output application/dw
, you'll see that it outputs the above. At this point, you can be sure you have the correct format needed to create a CSV (array of objects, in this case), so switch it back to output application/csv
and you should be good to go:
%dw 2.0
output application/csv
---
payload pluck($)
Upvotes: 2