We have a scenario where we need to concatenate all XML node values to String.
input XML
<root>
<line>1</line>
<line>2</line>
<line>3</line>
<line>4</line>
</root>
Output to String
1234
Please let me know how can i achieve in form of String.
Thanks in advance.
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 ($$ ++ $)
Try with this:
%dw 2.0
output application/json
---
{
result: payload.root.*line reduce ((item ,acc="") -> acc ++ item)
}
Related
I have a dataweave challenge which I couldn't figure out without making it complicated.
Input JSON payload
{
"Name" : "Mr.wolf",
"Age" : 26,
"Region" : "USA",
"SubRegion": "Pacific"
}
Output needed
{
"Name" : "Mr.wolf",
"Age" : 26
}
Here is a catch. I am not allowed to use the above shown output format structure in the transform message, or either remove the keys using "-" operation.
So basically, we shouldn't use the below dwl.
%dw 2.0
output application/json
---
(payload - "Region" - "SubRegion")
or
%dw 2.0
output application/json
---
{
"Name" : payload.Name,
"Age" : payload.Age
}
How can we achieve the required output by using Lambdas, Reduce, mapObject, functions or any other operation of choice, other than the restricted methods/usage shown above.
Any solution provided is much appreciated.
is this what you are looking for?
%dw 2.0
output application/json
---
payload filterObject ((value, key,index) -> (index <2 ))
Sounds like filterObject could work for you. Documentation
payload filterObject ((value, key) -> (key ~= "Name" or key ~= "Age"))
Another rendition of the same approach.
%dw 2.0
output application/json
---
payload mapObject {
(($$) : $) if (($$) ~= "Name" or ($$) ~= "Age")
}
The other rendition being:
%dw 2.0
output application/json
---
payload mapObject {
(($$) : $) if (($$$) < 2)
}
I have json payload as below:
Attributes:{
Contact:{
Address:{
PersonName:"John",
Line1:"sdsds",
Line2:"sdsdsd",
SubAddress:{
PersonName:"John",
Line1:"ggghg",
Line2:"xzxzxzx"
}
},
Phone:{
Home:"2323232323",
Office:"4545454545"
}
},
Id:"2343434",
order:"3343434"
}
I want to flatten the hierarchy to below output
Attributes:{
Contact.Address.PersonName:"John",
Contact.Address.Line1:"sdsds",
Contact.Address.Line2:"sdsdsd",
Contact.Address.SubAddress.PersonName:"John",
Contact.Address.SubAddress.Line1:"ggghg",
Contact.Address.SubAddress.Line2:"xzxzxzx",
Contact.Phone.Home:"2323232323",
Contact.Phone.Office:"4545454545",
Id:"2343434",
order:"3343434"
}
Attributes element can have any number of complex elements like the "Address" and "Contact". We will not know the key value of the complex elements at the point of coding. DWL should be able to produce a single level output. Want generic solution for this flattening using dw1 in Mule 3. Please help.
Below scripts recursively goes over the payload and construct single key-valued
pair object
%dw 1.0
%output application/json
%var payload = {"Attributes":{"Contact":{"Address":{"SubAddress":[{"PersonName1":"John","Line1":"ggghg","Line2":"xzxzxzx"},{"PersonName":"Jar","Line1":"ggghg","Line2":"xzxzxzx"}]},"Phone":{"Home":"2323232323","Office":"4545454545"}},"Id":"2343434","order":"3343434"}}
%function constructKeys(oldKeys, newKey) oldKeys ++ '.' ++ newKey
%function writeData(json ,output, keys) json match {
case is :null -> null,
case is :array -> {(json map ((item, index) -> writeData(item, output, keys ++ index)))},
case is :object -> {((json pluck $$) map writeData(json[$],output,constructKeys(keys,$)))},
default -> output ++ {(keys[1 to -1]): json }
}
---
Attributes: writeData(payload.Attributes,{}, '')
I have input payload coming like this -
[
{
"a": ""
},
{
"a": "abc"
},
{
"a": "pqr"
},
{
"a": "xyz"
}
]
and desired output is abc,pqr,xyz
I tried following dwl but couldn't succeed. Here is the code snippet
%dw 2.0
output application/json
query : payload filter ($.a != '') map (
$.a
)
Can someone please help me with the dataweave ? Thanks.
If your desired output is the string "abc,pqr,xyz":
%dw 2.0
output application/json
---
payload filter ($.a != "") map ($.a) joinBy ","
If you are trying to get the array ["abc", "pqr", "xyz"]:
Your code is fine...
%dw 2.0
output application/json
---
payload filter ($.a != "") map ($.a)
query: joinBy(payload.a filter $ !="", ',')
First select all 'a' fields to return new array of just values.
Filter the list for "".
Use joinBy function to append array values with a comma.
If I have xml like so....
<Root>
<Authority>Water</Authority>
<Sanctions>
<Sanction>
<SanctionCode>11</SanctionCode>
<SanctionDesc>First Sanction</SanctionDesc>
</Sanction>
<Sanction>
<SanctionCode>11</SanctionCode>
<SanctionDesc>Second Sanction</SanctionDesc>
</Sanction>
</Sanctions>
</Root>
Using DataWeave how can I create a json array of Santions using only the SanctionDesc?
I've tried this but it's not right...
%dw 1.0
%output application/json
---
records: payload.Root map {
Authority: $.Authority,
sanctions: $.Sanctions.Sanction map [$.SanctionDesc]
}
I want my output to look like this...
{
"records": [{
"Authority": "Water",
"sanctions": ["First Sanction", "Second Sanction"]
}]
}
Try this
%dw 1.0
%output application/json
---
records: {
Authority: payload.Root.Authority,
sanctions: payload.Root.Sanctions..SanctionDesc
}
Or
%dw 1.0
%output application/json
---
records: {
Authority: payload.Root.Authority,
sanctions: payload.Root.Sanctions.*Sanction map $.SanctionDesc
}
Hope this helps.
Understading the map operator is the key when picking and choosing elements from input payload. Map operator goes through all the array elements on the left hand side and we can pick the values from on right hand side, Giving example from Mulesoft portal
%dw 1.0
%output application/json
users: ["john", "peter", "matt"] map ((firstName, position) -> position ++ ":" ++ upper firstName)
Output:
{
"users": [
"0:JOHN",
"1:PETER",
"2:MATT"
]
}
See link below:
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#map
I am getting a field AnnualRevenue in the input, I have to look for the condition that if there is a decimal in that field I want to substing it to a whole number, but if it does not a decimal, it is passed without any transformation. Can one tell me how this could be achived in dataweave in mulesoft.
For another alternative, You can utilize regex:
%dw 1.0
%output application/java
---
{
result: "123.45" replace /(\.[0-9]*)/ with ""
}
Aside from subtring you can use the integer conversion of MEL/java by creating a global MEL function then use it in your dataweave expression.
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def removeDecimal(value) {
return (long) value
}
</global-functions>
</expression-language>
</configuration>
then in your dataweave expression:
%dw 1.0
%output application/json
---
{
"result": removeDecimal(-21.83)
}
Hope it helps.
Cheers
input:
<employees>
<employee>
<name>salumiah-syed</name>
</employee>
</employees>
%dw 1.0
%output application/xml
---
result: (payload.employees.employee.name splitBy "-") [0]
Or
%dw 1.0
%output application/java
---
{
value: "123.45",
result: ("123.45" as :number) as :string {format: "#"}
}
Reference: https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#coerce-to-string