concatenate XML values with different tag names using dataweave mule - mule

We have a scenario where we need to concatenate all XML node values to String.
input XML
<root>
<Address>
<line1>1</line1>
<line2>2</line2>
<line3>3</line3>
<line4>4</line4>
</Address>
<PostCode>
<line5>5<line5>
</PostCode>
</root>
Output to String
1 2 3 4 5
Please let me know how can i achieve in form of String.
Thanks in advance.

This question is already answered here concatenate XML values using dataweave mule
Referring DataWeave Reference Documentation at Reduce section:
Transform
%dw 1.0
%output application/json
---
concat: ["a", "b", "c", "d"] reduce ($$ ++ $)
Output
{
"concat": "abcd"
}
Therefore, you can try something like this: concat: payload.root.*line reduce ($$ ++ $)

Related

Date conversion in Mule

I am getting the date as below
2022-10-25 11:00:00
which I need to convert to
2022-10-25T11:00:00
Please let me know the appropriate data weave to achieve the above output.
#subhash,
you can try adding the letter "T" to the input string and then convert the whole string into DateTime as shown below.
*%dw 2.0
output application/json
(payload replace " " with "T") as DateTime*
%dw 2.0
output application/json
var data = "2022-10-25 11:00:00"
---
data replace " " with "T"

How to make orderBy() order numbers after alphabetic characters?

While trying to sort data in ascending order in DataWeave, I've noticed that if the input for orderBy() contains strings that start with numbers and alphabetic characters like ["6", "7", "a", "8"], then the result is ["6","7","8","a"].
Is there a way that we can have strings which are starting with alphabetic characters before numbers?
By default in Unicode digits are before alphabet characters. You can use the criteria parameter of orderBy() to change the way numbers are going to be compared with characters. For example adding character { as a prefix which is a character that is just ufter z (lowercase z), then numbers will be after any alphabet characters.
Example:
%dw 2.0
output application/json
import isNumeric from dw::core::Strings
---
payload orderBy (if (isNumeric($)) "{" ++ $ else $ )
Output:
[
"a",
"6",
"7",
"8"
]

How to format a number to 2 digits in mule4?

I have tried followings;
vars.counter as Number {format:'00'}
vars.counter as Number {format:'##'}
vars.counter as String {format:'00'}
vars.counter as String {format:'##'}
None of the above making 1 to 01
How can i do this in mule4?
Numbers (integers, floating point) don't have format DataWeave, like in many other languages. You have to convert to a String with the desired pattern. I tried the following combinations:
%dw 2.0
output application/json
---
[
1 as String {format:'##'},
1 as String {format:'00'},
1 as String {format:'#0'}
// , 1 as String {format:'0#'} ERROR!
]
Output:
[
"1",
"01",
"1"
]
Only the all zeros combination gives the desired result.

compare two arrays in dataweave2.0

i want code to compare two arrays and determine if they are equal or not irrespective of their order
[a,b,c] compared to [a, b,c ] should be true
[a,b,c] compare to [a,c,b] should be true as well.
i tried using the diff function from dataweave 2.0 but it works only if the parameters are Json objects not for arrays.
as #George mentioned a simple orderBy fixed my issue
import diff from dw::util::Diff
%dw 2.0
output application/json
---
{
result: diff(payload.array orderBy $, vars.array orderBy $).matches
}
fixed the issue.
You can use the Diff module with the unordered property
import diff from dw::util::Diff
%dw 2.0
output application/json
---
{
result: diff(payload.array, vars.array, {unordered: true}).matches
}

Find substring in array

I want to count the count the number occurences of a substring in a field in an array.
Example:
The XML below has 3 occurences of the substring 'TXT1' in Field1.
<Level1>
<Level2>
<Field1>10000TXT1</Field1>
<Field1>TXT210000</Field1>
<Field1>10001TXT1</Field1>
<Field1>TXT30000</Field1>
<Field1>10TXT1000</Field1>
<Field1>TXT20000</Field1>
</Level2>
fun countOccurences(txtToSearchFor) =
// Some code that can count how many times the text 'TXT1' occur in all the Field1 fields.
I have tried the examples below, but they dont work
1)
trim(upper(Field1)) contains "TXT1"
2)
(((Field1) find 'TXT1') joinBy '')
Hope you can help :-)
Hi you can use the function sumBy from the dw::core::Arrays module. This function takes an array and a lambda that returns the number to be added for each element in the array. So then I just need to ask for the times of repetitions of a String inside another String. That is achieved by using sizeOf and find
%dw 2.0
output application/json
import sumBy from dw::core::Arrays
fun timesOf(value: Array<String>, txtToSearchFor: String) =
value sumBy ((text) -> sizeOf(text find txtToSearchFor))
---
payload.Level1.Level2.*Field1 timesOf "TXT1"
I found the answer: :-)
fun countOccurences(texts) =
sizeOf (Level1.Level2.*Field1 filter ($ contains texts))