json - Should I go with if-else or match-case in the below scenario? - Stack Overflow

I have my input as json, where I have to derive the quantity based on one field shippingNode.Mapping:

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

  1. If shippingNode is starting with ‘RL’ or ‘RS’, map as per mapping to rlQuantity
  2. If shippingNode is 'WS1002', map as per mapping to wsQuantity else pass '0'
  3. If shippingNode is starting with ‘AS’, map as per mapping to asQuantity else '0'
  4. If shippingNode value does not match any of these condition, then as per mapping, map to okQuantity

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

  1. If shippingNode is starting with ‘RL’ or ‘RS’, map as per mapping to rlQuantity
  2. If shippingNode is 'WS1002', map as per mapping to wsQuantity else pass '0'
  3. If shippingNode is starting with ‘AS’, map as per mapping to asQuantity else '0'
  4. If shippingNode value does not match any of these condition, then as per mapping, map to okQuantity

Input

{
  "data": {
    "shippingNode": "0200",
    "availableQuantity": {
      "supplyQuantity": 2
    }
  }
}

Output: { "okQuantity": 2}

Share Improve this question asked Mar 28 at 7:49 MyDreamMyDream 358 bronze badges 2
  • I don't know dataweave or mule, but in general: Often, there is a third choice: Use a dictionary/associative container that maps the inputs to outputs. That is a specific case of a general choice between modeling relations in code vs. modeling them in data structures. (Admittedly, an associative container is a little less elegant if not every input can be mapped to an output, as in your case, so that there is a default.) – Peter - Reinstate Monica Commented Mar 28 at 7:57
  • What does "map as per mapping " even means? Key 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 of supplyQuantity ? – aled Commented Mar 28 at 11:30
Add a comment  | 

1 Answer 1

Reset to default 0

Both 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
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744050076a4549910.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信