I want to transform message in mule application based on some condition.
%dw 1.0
%output application/json
---
{
**// create the below if the size of payload is greater than 0**
resource_type : "speciality",
speciality_name : payload[0].speciality_name,
speciality_description : payload[0].speciality_description,
uuid : payload[0].uuid
**//else create the below message**
message : "No records were fetched from backend"
}
Could any one please kindly help me on this?.
You can use when otherwise condition for this. Like
%dw 1.0
%output application/json
---
{
// create the below if the size of payload is greater than 0**
resource_type : "speciality",
speciality_name : payload[0].speciality_name,
speciality_description : payload[0].speciality_description,
uuid : payload[0].uuid
} when payload != null and (sizeOf payload) > 0 otherwise
{
//else create the below message**
message : "No records were fetched from backend"
}
Hope this helps.
Related
Trying to convert the below dataweave from 1.0 to 2.0, but everything I've tried gives the following errors like
SkipNullon shows error
Usage of Namespacing is not accepted,
#PostalCode[0..4] is not valid and
can't access the value inside insuredInfo using insuredPrimaryAddr.
Dataweave 1.0:
%dw 1.0
%output application/xml skipNullOn="everywhere"
%var insuredInfo = payload.DTOApplication.DTOInsured
%var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.#PartyTypeCd == "InsuredParty")].*Addr[?($.#AddrTypeCd == "InsuredPrimaryBusAddr")][0]
%namespace ns0 http://decisionresearch.com/RateMaker
---
ns0#rate:{
ns0#code:insuredPrimaryAddr,
ns0#ZipCode: payload..Addr[?($.#AddrTypeCd == "InsuredPrimaryBusAddr")][0].#PostalCode[0..4] default ""
}
I tried Dataweave 2.0:
%dw 2.0
output application/xml skipNullOn="everywhere"
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.#PartyTypeCd == "InsuredParty")].*Addr[?($.#AddrTypeCd == "InsuredPrimaryBusAddr")][0]
namespace ns0 http://decisionresearch.com/RateMaker
---
ns0#rate:{
ns0#code:insuredPrimaryAddr,
ns0#ZipCode: payload..Addr[?($.#AddrTypeCd == "InsuredPrimaryBusAddr")][0].#PostalCode[0..4] default ""
}
payload : https://github.com/Manikandan99/rate-dtostep/blob/master/response.xml
Any ideas please on how to write the same in dataweave 2.0?
In dataweave 2.0 we use to operator for array indexing.
%dw 2.0
output application/xml skipNullOn="everywhere"
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.#PartyTypeCd == "InsuredParty")].*Addr[?($.#AddrTypeCd == "InsuredPrimaryBusAddr")][0]
ns ns0 http://decisionresearch.com/RateMaker
---
ns0#rate:{
ns0#code:insuredPrimaryAddr,
ns0#ZipCode: payload..Addr[?($.#AddrTypeCd == "InsuredPrimaryBusAddr")][0].#PostalCode[0 to 4] default ""
}
Output
<?xml version='1.0' encoding='windows-1252'?>
<ns0:rate xmlns:ns0="http://decisionresearch.com/RateMaker">
<ns0:ZipCode>80003</ns0:ZipCode>
</ns0:rate>
I feel like I'm losing my mind because I can't figure out how to do something as simple as iterate over an object, concatenate a plain text string, and output the result to a variable. Here is something similar I've done which works fine:
%dw 2.0
output application/xml
var promptParams = attributes.queryParams filterObject($$ startsWith "PROMPT") orderBy($$)
---
{
RESULT: {
Prompts:
promptParams mapObject(promptValue, promptName, index) -> {
PROMPT: {
UniquePromptName: promptName,
FieldValue: promptValue
}
}
}
}
So in this case I filter the url query string parameters to get only the ones I want, then I iterate over those and construct the xml output. The problem I'm having is that if I try to do the same thing but output a plain text string to a variable, I can't get anything to work.
Basically what I want is to go from this input:
https://example.com?PROMPT1=foo&PROMPT2=bar&PROMPT3=lorem&PROMPT4=ipsum&utm_source=Dolor&utm_campaign=SitAmet
to this output stored in a flow variable:
foo!bar!lorem!ipsum
I must be missing something basic because it can't be that hard to accomplish this. What am I doing wrong?
It should be something like this:
%dw 2.0
output text/plain
var promptParams = attributes.queryParams filterObject($$ startsWith "PROMPT")
---
promptParams pluck($) reduce ($$ ++ "!" ++ $)
Output:
foo!bar!lorem!ipsum
You asked for text plain, but I would recommend application/java if you are usign the variable inside the flow.
%dw 2.0
output text/plain
var promptParams = (((payload.message splitBy "?")[1]) splitBy "&") //stored url //in payload.message
---
promptParams map {
a: ($ splitBy "=")[1]
}.a joinBy "!"
you can use pluck and joinBy and just make sure that you have the target set to a variable if you are using the transform message component.
<ee:transform doc:name="Transform Message" >
<ee:variables >
<ee:set-variable variableName="promptAttributes" ><![CDATA[%dw 2.0
output text/plain
---
(attributes.queryParams[?($$ startsWith "PROMPT")] pluck $) joinBy "!"]] </ee:set-variable>
</ee:variables>
</ee:transform>
I am trying to concatenate strings in mule Transform message like below, but I am getting the below exception at run time. Could anyone please kindly help me on this? I am new to mule as well.
%dw 1.0
%output application/json
---
{
references : "" when payload[0].person_id==null otherwise "person/"+payload[0].person_id,
}
Exception ::
Root Exception stack trace:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing:
references : "" when payload[0].person_id==null otherwise "person/"+payload[0].person_id,
^
Type mismatch for '+' operator
found :string, :number
use ++ operator which is the concatenating operator in Dataweave.
Giving below the example from Mulesoft documentation
%dw 1.0
%output application/json
---
{
name: "Mule" ++ "Soft"
}
for more please read below documentation
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#concat
Refer Mule Documentation for Mule Dataweave operators. For concatenating string ++ operator should be used. Like
%dw 1.0
%output application/json
---
{
references : "" when payload[0].person_id==null otherwise "person/" ++ payload[0].person_id
}
Hope this helps.
%dw 1.0
%output application/json
---
{
name: payload.firstName ++ payload.lastName
}
Make sure that any of the values (firstname or lastName) is not null.
I am processing a file where every other line is blank, how to get rid of these lines using dataweave or groovy?
My payload now looks like this
my transformer which is parsing the lines is:
%dw 1.0
%output application/java
---
payload map
{
line: $[0]
}
Thanks for the response
Try the same dataweave with filtering the payload. Check if anything in the incoming payload is empty or null kind of thing which is causing the issue.
%dw 1.0
%output application/java
---
payload filter ($ !=null and $ !='') map
{
line: $[0]
}
Going through the Dataweaver documentaion
Link:https://developer.mulesoft.com/docs/dataweave#_attribute_selector_expressions
Section 3.4 Key Present
Trying out the example provide below .
Input:
<users>
<name>Mariano</name>
<name>Luis</name>
<name>Mariano</name>
</users>
Transform:
%dw 1.0
%input payload application/xml
%output application/xml
---
users: payload.users.name[?($ == "Mariano")]
when I try to give this expression in my DataWeaver it gives warning like cannot coerce a:string to a: array:(7,92).
Have given the same way mentioned in the document. Could anyone please advice.
Expected Response:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<name>Mariano</name>
<name>Mariano</name>
</users>
Also in the document 1.1.2 string manipulation example wasn't working for me
%dw 1.0
%input payload application/xml
%output application/json
%function words(name) name splitBy " "
---
contacts: payload.users.*user map using (parts = words($.name)){
firstName: parts[0],
(secondName: parts[1]) when (sizeOf parts) > 2,
lastName: parts[-1],
email: "$((lower $.name) replace " " with ".")#acme.com.ar",
address: $.street
}
showing error like multiple marker at this line missing '}' no viable alternative at input email
Started learning and working out the examples provided. Thanks.
The example in the docs has a typo, there is an * missing before name (it should be fixed):
%dw 1.0
%input payload application/xml
%output application/xml
---
users: payload.users.*name[?($ == "Mariano")]
The problem is that XML doesn't have a built-in list representation, so the list is represented by multiple occurences of a tag. The expression *name returns a list with the occurrences of name, the expression [?($ == "Mariano")] it's like a filter (I prefer filter since it is easier to understand).
The cryptic error message appears because the operator applies to a list, but payload.users.name returns the first appeareance of name. (That's why it says cannot coerce string to array).