I have my input as json, where I have to derive the quantity based on one field shippingNode
.
Mapping: data/availableQuantity/supplyQuantity
The conditions are as below
- If
shippingNode
is starting with ‘RL’ or ‘RS’, map as per mapping torlQuantity
- If
shippingNode
is 'WS1002', map as per mapping towsQuantity
else pass '0' - If
shippingNode
is starting with ‘AS’, map as per mapping toasQuantity
else '0' - If
shippingNode
value does not match any of these condition, then as per mapping, map tookQuantity
Input
{
"data": {
"shippingNode": "0200",
"availableQuantity": {
"supplyQuantity": 2
}
}
}
Output: { "okQuantity": 2}
I have my input as json, where I have to derive the quantity based on one field shippingNode
.
Mapping: data/availableQuantity/supplyQuantity
The conditions are as below
- If
shippingNode
is starting with ‘RL’ or ‘RS’, map as per mapping torlQuantity
- If
shippingNode
is 'WS1002', map as per mapping towsQuantity
else pass '0' - If
shippingNode
is starting with ‘AS’, map as per mapping toasQuantity
else '0' - If
shippingNode
value does not match any of these condition, then as per mapping, map tookQuantity
Input
{
"data": {
"shippingNode": "0200",
"availableQuantity": {
"supplyQuantity": 2
}
}
}
Output: { "okQuantity": 2}
1 Answer
Reset to default 0Both options with match
or if
are valid.
Here my preffered choice using match
%dw 2.0
output application/json
var mapping = payload.data.availableQuantity.supplyQuantity
---
(payload.data.shippingNode) match {
case w if((w startsWith "RL") or (w startsWith "RS")) -> {
rlQuantity: mapping
}
case "WS1002" -> {
wsQuantity: mapping
}
case v if((v startsWith "AS")) -> {
asQuantity: mapping
}
else -> {
okQuantity: payload.data.availableQuantity.supplyQuantity
}
}
Here with if
%dw 2.0
output application/json
var mapping = payload.data.availableQuantity.supplyQuantity
var shippingNode = payload.data.shippingNode
---
if( (shippingNode startsWith "RL") or (shippingNode startsWith "RS") )
{
rlQuantity: mapping
}
else if( (shippingNode == "WS1002" ) )
{
wsQuantity: mapping
}
else if( (shippingNode startsWith "AS") )
{
asQuantity: mapping
}
else
{
okQuantity: mapping
}
supplyQuantity
which is mentioned as the "mapping" has a numeric value, not a map. The explanation is confusing. The single example doesn't cover all cases. Do you mean to output a key X with the value ofsupplyQuantity
? – aled Commented Mar 28 at 11:30