Grouping the message based on one field in mule data weaver - mule

I have a requirement where I want to filter the incoming message based on the category='E'. So if the category = 'I', it should be trimmed off the message.
Sample Input
<DataSet>
<SubDataSet>
<DataNum>TEXU9022186</DataNum>
<Category>E</Category>
</SubDataSet>
<SubDataSet>
<DataNum>TEXU9022186T</DataNum>
<Category>I</Category>
</SubDataSet>
<SubDataSet>
<DataNum>TEXU9022186T</DataNum>
<Category>E</Category>
</SubDataSet>
</DataSet>
Sample Output
<DataSet>
<SubDataSet>
<DataNum>TEXU9022186</DataNum>
<Category>E</Category>
</SubDataSet>
<SubDataSet>
<DataNum>TEXU9022186T</DataNum>
<Category>E</Category>
</SubDataSet>
</DataSet>
Could you please how to achieve this by mule dataweaver

Use filter in dataweave script as
%dw 1.0
%output application/xml
---
{DataSet : payload.DataSet.*SubDataSet filter ($.Category == "E")}
Hope this helps..

Related

Convert file to JSON in Mule 4

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.

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

How to check NaN in mule

How to check NaN value using xpath3 in mule.
I tried to do this but not getting result correct
#[xpath3('total',payload,'NUMBER')!='NaN'?xpath3('total',payload,'NUMBER'):0]
But not working
How can I check NaN value in mule?
Try using is or instance of, for example #[xpath3('total',payload,'NUMBER') is java.lang.Number ? xpath3('total',payload,'NUMBER') : 0]
Based on further testing, by using the following example:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<total>100</total>
</data>
We can utilize isNaN() method, because the payload is a java.lang.Double. Therefore we can use the following MEL: #[xpath3('//total',payload,'NUMBER').isNaN() ? 0 : xpath3('//total',payload,'NUMBER')]
It can be tested through the following simple flow:
<flow name="simpleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger message="#[xpath3('//total',payload,'NUMBER').isNaN() ? 0 : xpath3('//total',payload,'NUMBER')]" level="INFO" doc:name="Logger"/>
</flow>
Change the value of total field with a non numeric for testing purpose, e.g.: <data><total>hundred</total></data>

Mule DataWeave Group by And Sum

I am doing something rather simple and hope that I am just missing a simple statement, I am trying to do a group by and sum but can not seem to be able to make DataWeave complete the task for me. Any help would be much appreciated!
I have the following XML
<?xml version="1.0" encoding="UTF-8"?>
<getRecordsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<getRecordsResult>
<company>0a6979a72ba5020092d4a5a667da15ac</company>
<u_billed_amount>504</u_billed_amount>
</getRecordsResult>
<getRecordsResult>
<company>548839a72ba5020092d4a5a667da15e0</company>
<u_billed_amount>29.17</u_billed_amount>
</getRecordsResult>
<getRecordsResult>
<company>0a6979a72ba5020092d4a5a667da15ac</company>
<u_billed_amount>33.75</u_billed_amount>
</getRecordsResult>
And the Following in a dataWeave expression -
%dw 1.0
%output application/xml
%namespace ns0 http://www.service-now.com/u_incident_task
---
results: {(
payload.getRecordsResponse.*getRecordsResult groupBy $.company map {
amtToBill: ($.u_billed_amount)
}
)}
Which yields the desired grouping, however I need to Sum the amount billed.
<?xml version='1.0' encoding='UTF-8'?>
<results>
<amtToBill>
<u_billed_amount>29.17</u_billed_amount>
</amtToBill>
<amtToBill>
<u_billed_amount>504</u_billed_amount>
<u_billed_amount>33.75</u_billed_amount>
</amtToBill>
</results>
What I need is the following
<?xml version='1.0' encoding='UTF-8'?>
<results>
<amtToBill>
<u_billed_amount>29.17</u_billed_amount>
</amtToBill>
<amtToBill>
<u_billed_amount>537.75</u_billed_amount>
</amtToBill>
</results>
I know there has to be a summation in dataWeave, I have not found it yet.
I found my own answer, just do this -
amtToBill: sum $.u_billed_amount

How to Extract Text Node Value From Payload Using XPath in MEL

I want to extract some text values from an message's XML payload so that I can use them in a jdbc query.
Given the test XML file below I want to obtain the string value of first book's author text node.
Something like:
INSERT INTO books VALUES (#[xpath('/catalog/book[0]/author/text()')])
To test the expression I am just using a logger but can't seem to get it to extract correctly.
<logger message="#[xpath('/catalog/book[0]/author/text()')]" level="DEBUG" doc:name="Logger"/>
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>
Here is the correct MEL expression:
#[xpath('/catalog/book[1]/author/text()').text]
Note in XPath, the first node is 1 not 0.
David's answer worked fine except for one thing. For me, .text didn't work. I had to use .wholeText. This is probably because I'm using a xerces implementation somewhere in my work project.