Hi i am trying to write a conditional expression instead of using when in data weave. I am not getting how to achieve this.
I am trying to write something as mention below to filter condition with in the statement:
%dw 1.0
%output application/java
---
{
data: payload,
paylo: (sizeOf payload) < 20 ? 90:0
}
The below code would help you for applying the condition
%dw 1.0
%output application/java
---
{
data: payload,
paylo: 90 when sizeOf payload < 20 otherwise 0
}
The documentation should help you understand sizeOf and conditional statements
Related
I have a Json Input like below, where if doa is null or "" it needs to replace with the value test if it contains Value then no change required. I have achieved in dwl 2 version using update. Using mabObject in dataweave 1.0 version, but it is not working out. Looking for your expertise. Thanks
Input:
{
"sets": {
"exd": {
"cse": {
"doa": null
}
}
}
}
Achieved in dwl 2 :
%dw 2.0
output application/json skipNullOn="everywhere"
import * from dw::util::Values
---
if
(!isEmpty (payload.sets.exd.cse.doa )) payload
else
payload update
{
case .sets.exd.cse.doa! -> "test"
}
Response
{
"sets": {
"exd": {
"cse": {
"doa": "test"
}
}
}
}
Looking for Mule 3 dataweave 1.0 version of it??
This is a lesser backport of the update operator in DataWeave 2.0 to DataWeave 1.0 as a function. The keys parameter is the sequence of keys as an array as strings. I added a check for key null or empty as it can be done in DataWeave 1.0.
%dw 1.0
%output application/json
%function updateKey(o,keys,value)
o match {
:object -> o mapObject ($$):
updateKey($, keys[1 to -1], value )
when ($$ ~= keys[0] and ((sizeOf keys) >1))
otherwise ($
when ((sizeOf keys) >1) otherwise value
),
default -> $
}
---
updateKey(payload, ["sets", "exd", "cse", "doa"], "test") when (payload.sets.exd.cse.doa is :null or payload.sets.exd.cse.doa == "") otherwise payload
If / else statements work considerably differently in dataweave 1.0, update doesn't exist, and I don't know if isEmpty exists as part of core - don't think so. If you know ahead of time the structure of the object, you can simply build a new object like this:
%dw 1.0
%input payload application/json
%output application/json
---
payload when (payload.sets.exd.cse.doa != '' and payload.sets.exd.cse.doa != null)
otherwise {
sets: {
exd: {
cse: {
data: "test"
}
}
}
}
As per my comment to #aled's solution, here is an update that will limit the change to the specific key even if there are multiple key-value pairs at the target level:
%dw 1.0
%output application/json
%function updateKey(o,keys,value)
o match {
:object -> o mapObject ($$):
updateKey($, keys[1 to -1], value )
when ($$ ~= keys[0] and ((sizeOf keys) >1))
otherwise ($ when (((sizeOf keys) >1) or '$$' != keys[0])
otherwise value
),
default -> $
}
---
updateKey(payload, ["sets", "exd", "cse", "doa"], "test") when (payload.sets.exd.cse.doa is :null or payload.sets.exd.cse.doa == "") otherwise payload
I have divided my dataweave script into modules, and I have used skipNullOn="everywhere" in the main dwl, so all the null values in all the modules are skipped. But, I don't want to skip the null values of a particular modules. How do I override(nullify) the skipNullOn="everywhere" for that particular module.
Input:
<XML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ABC xsi:nil="true"/>
<DEF/>
</XML>
dataweave code:
%dw2.0
output application/json skipNullOn="everywhere"
---
payload.XML
Expected Output(
json):
{
"ABC": ""
}
Getting Output(json):
{
}
Since I am answering it pretty late, I am not sure if this of much help to you. Anyways, you can have the list of nodes for which you want to skip the 'skipNullOn' check in a comma separated format in the property file. And then you try something similar as I have here below, which will help you iterate over all the nodes and then achieve the output as you desire:
%dw 2.0
output application/json skipNullOn="everywhere"
var toSkipNullOn='ABC,XYZ'
fun checkNull(key,val) = if((toSkipNullOn splitBy(',')) contains(key as String)) '' else null
---
payload.XML mapObject (v0, k0, i0) ->
{
(k0):checkNull(k0,v0)
}
In this example I have hardcoded the node names (ABC,XYZ) to the variable toSkipNullOn. Instead of that you'll have to read it from the property file as p('key-name') and assign it to toSkipNullOn.
You need to explicitly write the logic for that field, should be something like this
%dw2.0
output application/json skipNullOn="everywhere"
---
{
"ABC": if (payload.XML.ABC_val !=null ) else ""
}
You can try this workaround, to get the expected result. Use two dataweave
1st Dataweave (wherever you get "nil", syntax need to be updated as the below one)
%dw 2.0
output application/xml skipNullOn="everywhere"
ns xsi http://www.w3.org/2001/XMLSchema-instance
---
{
ABC #( xsi#nil:"true"): payload.ABC ,
DEF : payload.DEF
}
2nd Dataweave
%dw 2.0
output application/json
---
payload
You can then replace null with "" easily.
I am adding the elements of a payload in Dataweave 1.0 as below :
%var summation =[[]]
summation :sum ((schemaInput map ($.BenefitLimit when $.BenefitLimit !="" otherwise (0 as :number)))) ,
Can anyone help me to replicate the same logic in Dataweave 2.0 ??
Thanks
Here is a rough conversion without knowing the context.
A few points on the changes:
dw 2.0 header
fun or var instead of %var - no need for %
when/otherwise in dw 2 is now if/else
use sum instead of :sum - no need for : anymore with operators and functions
%dw 2.0
output application/java
var data= [ {"BenefitLimit": "10"}, {"BenefitLimit": "20"}]
fun summation(schemaInput) = sum ((schemaInput map ((if ($.BenefitLimit == "") 0 else $.BenefitLimit as Number))))
Documentation on migration here: https://docs.mulesoft.com/mule-runtime/4.1/migration-dataweave
how to remove specific fields from map using dataweave
input:
{
a:1,
b:2,
c:3,
d:4
}
I want to remove c and d fields(c and d values are dynamic) and display only
output
{
a:1,
b:2
}
How can we do it in data weave
According to the Dataweave Reference Documentation, you can remove a field from an object. Try using this:
%dw 1.0
%output application/json
---
payload - "c" - "d"
Below code works fine:
%dw 2.0
var arr=["c","d"]
output application/json
---
payload filterObject ((value, key, index) -> !(arr contains (key) as String))
You can add/remove the keys that you want to exclude in the variable 'arr'.
i want to Change the time "2017-08-22T17:10:12Z" into "20170822".
So i used this substring function but its throw errors continuously.
%dw 0.1
%output application/xml
---
po: {
var:payload.po.ordered_date,
Date: substring(var,2,3)
}
There is no function called substring in Dataweave instead use as given below.
%dw 0.1
%output application/xml
po: {
var:payload.po.ordered_date,
Date: substring(2..3)
}
Or you can define a Java function substring in global xml config file and use it as you have given above.
Variable declaration has syntax error. Also in dataweave substring doesnt work as mentioned. Please refer following code
%dw 0.1
%output application/xml
%var variable = payload.po.ordered_date
---
po: {
Date: variable as :localdatetime as :string {format:"yyyyMMdd"}
}
Hope this helps.
I use Groovy Script to retrieve the substring. Try like below.
def testString = 'ABC';
flowVars['subStringedString']= flowVars.testString.substring(0,2);
The above will give you AB.
You can access this substring as flowVars['subStringedString'] in Mule flow
There is no substring operator in Dataweave and the way you to do this is like var[1..3] is you want get 4 characters from 2nd position. Should be something on lines below
%dw 0.1
%output application/xml
---
po: {
var:payload.po.ordered_date,
Date: var[1..3]
}