Convert file to JSON in Mule 4 - mule

I extracted a file that comes in a zip and looks similar to this:
Payload =
<?xml version="1.0" encoding="UTF-8"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><dimension ref="A1"/><sheetViews><sheetView workbookViewId="0" tabSelected="true"/></sheetViews><sheetFormatPr defaultRowHeight="15.0"/><sheetData>
<row r="1">
<c r="A1" t="inlineStr"><is><t>First</t></is></c><c r="B1" t="inlineStr"><is><t>Middle</t></is></c><c r="C1" t="inlineStr"><is><t>Last</t></is></c><c r="D1" t="inlineStr"><is><t>SSN</t></is></c><c r="E1" t="inlineStr"><is><t>Street</t></is></c><c r="F1" t="inlineStr"><is><t>MailingState</t></is></c><c r="G1" t="inlineStr"><is><t>Code</t></is></c><c r="H1" t="inlineStr"><is><t>MailingCountry</t></is></c><c r="I1" t="inlineStr"><is><t>Birthdate</t></is></c><c r="J1" t="inlineStr"><is><t>name</t></is></c><c r="K1"
<row r="2">
<c r="A2" t="inlineStr"><is><t>William</t></is></c><c r="A5" t="inlineStr"><is><t></t></is></c><c r="B2" t="inlineStr"><is><t>William</t></is></c><c r="D2" t="inlineStr"><is><t>123456798</t></is></c><c r="E2" t="inlineStr"><is><t>Test</t></is></c><c r="F2" t="inlineStr"><is><t>XX</t></is></c><c r="G2" t="inlineStr"><is><t>12345</t></is></c><c r="H2" t="inlineStr"><is><t></t></is></c><c r="I2" t="inlineStr"><is><t>1992-13-11T04:00:00</t></is></c><c r="J2"
mediaType = application/java; charset=UTF-8
How can I convert that file to something that I can manipulate better, for example JSON.
Mule Runtime version 4.4.0 EE

Use the read() function to parse it as the XML file the content appears to be, then transform to whatever you want.
Example DataWeave transformation:
output application/json
---
read(payload, "application/xml")
Note that the payload seems truncated. If it is really XML above script will work. Also I recommend to perform any transformation before outputting to JSON, to avoid performance issues. If you are doing more transformations later in the flow is better to output to application/java and convert to JSON in the last transformation.

Related

Exception cx_st_match_element when deserializing XML?

I'm having trouble getting a simple transformation for XML to work in ABAP. I keep getting the exception cx_st_match_element when I try to execute it on a test XML document inside of a report.
I have the following XML that I want to transform into an ABAP internal table:
<?xml version="1.0" encoding="UTF-8"?>
<studenten xmlns="http://www.foo.be/bar/preinschrijvingsflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.foo.be/bar/preinschrijvingsflow bar_studenten.xsd">
<student>
<barGuid>1</barGuid>
<familienaam>Doe</familienaam>
<voornaam>John</voornaam>
<geslacht>1</geslacht>
<nationaliteit>BE</nationaliteit>
<geboortedatum>1995-11-18</geboortedatum>
<geboorteplaats>Antwerpen</geboorteplaats>
<email>John.Doe#gmail.com</email>
<straatNummer>Grote Markt 1 bus 0102</straatNummer>
<postcode>1000</postcode>
<gemeente>Brussel</gemeente>
<land>BE</land>
<telefoonnummer>+32123456789</telefoonnummer>
<academiejaar>2021</academiejaar>
</student>
</studenten>
To this end I defined the following simple transformation I called zc_tr_student:
<?sap.transform simple?>
<tt:transform
xmlns="http://www.foo.be/bar/preinschrijvingsflow"
xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:ddic=" http://www.sap.com/abapxml/types/dictionary">
<tt:root name="studenten" type="ddic:ZCTT_bar_STUDENT"/>
<tt:template>
<studenten>
<tt:loop ref=".studenten" name="studenten">
<student>
<barGuid tt:value-ref="$studenten.bar_guid"/>
<familienaam tt:value-ref="$studenten.familienaam"/>
<voornaam tt:value-ref="$studenten.voornaam"/>
<geslacht tt:value-ref="$studenten.geslacht"/>
<nationaliteit tt:value-ref="$studenten.nationaliteit"/>
<geboortedatum tt:value-ref="$studenten.geboortedatum"/>
<geboorteplaats tt:value-ref="$studenten.geboorteplaats"/>
<email tt:value-ref="$studenten.email"/>
<straat_nummer tt:value-ref="$studenten.straat_nummer"/>
<postcode tt:value-ref="$studenten.postcode"/>
<gemeente tt:value-ref="$studenten.gemeente"/>
<land tt:value-ref="$studenten.land"/>
<telefoonnummer tt:value-ref="$studenten.telefoonnummer"/>
<academiejaar tt:value-ref="$studenten.academiejaar"/>
</student>
</tt:loop>
</studenten>
</tt:template>
</tt:transform>
In the tt:value-refattributes I refer to the field in the DDIC line type of the ABAP internal table corresponding to the tag in the XML.
If I call this simple transformation from an ABAP report like this:
call transformation zc_tr_student
source xml lv_bxml
result studenten = p_student.
The cx_st_match_element is thrown.
I validated both the syntax of the file and its adherence to the schema. The XML file and the XSD file are present in the same directory on the application server. I have no idea why the ST fails as the cx_st_match_element instance does not have any useful information except that it expected a studenten element. That element is clearly present in the XML file as the root element.
I'm inexperienced with defining simple transformations and I can't spot my error myself. Thank you in advance for your help,
Joshua

Karate "match contains" for xml does not work like JSON [duplicate]

This question already has an answer here:
How to compare XML response with Json in Karate
(1 answer)
Closed 1 year ago.
I've noticed that "match contains" for checking if xml structure contains in another xml structure works differently than json counterpart. To give you an example :
* def expectedResult =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
<id>1</id>
<id>2</id>
<id>3</id>
</response>
"""
* header Accept = xml
Given path 'some\path'
When method GET
Then status 200
## If response contains -
## <response><id>1</id></response>
## The following match will fail with -
## "actual and expected arrays are not the same size - 1:3"
And match response contains expectedResult
If it's a JSON structure, it will pass because 1 contains in the list of 1,2,3. But in XML, it's still checking array size. Is that how it suppose to work?
Update : This is not the same question as the other question. I'm trying to compare reference XML file that I have with XML structure being returned from the API. The question you linked to is comparing JSON reference file to XML response. So, that begs the question, in karate, do I have to convert my reference XML and response XML to json just to perform "contains"?
XML has some fundamental "shape" differences from JSON. I leave it to you to figure out, one of the challenges is the concept of a "root" tag, which JSON does not have. So they will not "match up" the way you may expect.
One tip - convert XML to JSON if in doubt. Here is an example:
* def expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
<id>1</id>
<id>2</id>
<id>3</id>
</response>
"""
* json temp = $expected/response
* match temp.response.id == ['1', '2', '3']

How to combine value from XML with a String?

I have a listener that receives a XML payload. In the following transform I would like to combine a string and a value this XML, but it breaks
This is the XML I receive:
<?xml version="1.0" encoding="UTF-8"?>
<INVOIC02>
<IDOC BEGIN="1">
<RESULT>12345</RESULT>
</IDOC>
</INVOIC02>
This is my transformation:
%dw 2.0
output text/plain
---
"Result:" ++ (payload.INVOICE.IDOC.RESULT)
Apparently I access the payload wrong, I guess. The error message looks like this:
You called the function 'Value Selector' with these arguments:
1: String ("\n\n ...)
2: Name ("INVOICE")
But it expects one of these combinations:
(Array, Name)
...
Any idea what I'm doing wrong?
The problem is that the xml doesn't have the mimetype set. As a workaround set the payload with value payload and set also the mimetype to xml

Mule: DataMapper - CSV to XML Conversion , Final XML is not formatted with root element

I'm new to mule, working with Matamapper which converts csv file to xml. I'm able to convert csv to xml . But the final output response is not formatted with actual root element. Please find my final output xml structure. It doesnt have root element. Kindly guide me . If this is correct, help me how to insert root element. ( Bcz i tried Set payload to insert the root element) it is not working correclty.
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<name>jothi</name>
<street>123, main</street>
</employees>
<employees>
<name>Jane </name>
<street>Main Street</street>
</employees>
Also referred : csv to xml: not sure the best way to do it in Mule ESB
http://www.mulesoft.org/documentation/display/current/DataMapper+Flat-to-Structured+and+Structured-to-Flat+Mapping - giving errors in datamapper
Thank you.
Upgrading MuleStudio to 3.5 cleaned up a lot of the errors I was seeing using the DataMapper

Parsing using NSXMLParser

I am being returned some XML from a web service. Basically, the xml looks like this:
<response>
<data>
More XML here but the it's escaped by XML entities
</data>
</response>
so, as you can see, I have xml that is valid, but the stuff inside data tag is escaped with XML entities. what's the best (most efficient) way for me to feed this into the parser?
What I am doing right now is, when I get the data from web service, I convert it into NSString....then replace the "XML escaped entities" with real ones.....then convert it back into NSData...then feed it into the parser. This doesn't seem like a very good solution so I was wondering if there's a better way to do it?
Thanks.
Alright, here's the xml that I am getting:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><ActivityId CorrelationId="d39007b5-ee69-41c7-a61d-831b456f9ea3" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">aa88d1cd-253c-48d1-abeb-62a880bea806</ActivityId></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><LoginResponse xmlns="http://MSS"><LoginResult><LoginInformation>
<User>
<UserID>612</UserID>
<UserName>Demo User</UserName>
<Email>mssdev#mss-mail.com</Email>
<CompanyID>17034</CompanyID>
<CompanyName>PlanET Demonstration Agency</CompanyName>
</User>
</LoginInformation></LoginResult></LoginResponse></s:Body></s:Envelope>
As you can see, everything in is escaped.
That's kind of horrifying. Why don't you just take the contents of that tag (e.g. the data with the entities) and just pass that through another NSXMLParser? The first parser will have decoded the entities, and the second parser will be presented with the decoded version of the contents of that tag.