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

  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
    }

本文标签: jsonShould I go with ifelse or matchcase in the below scenarioStack Overflow