admin 管理员组文章数量: 1086019
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
}
本文标签: jsonShould I go with ifelse or matchcase in the below scenarioStack Overflow
版权声明:本文标题:json - Should I go with if-else or match-case in the below scenario? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744050077a2524854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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