仅在值符合特定条件时才包括键

时间:2019-05-21 00:06:35

标签: anypoint-studio dataweave mulesoft

我有这段Dataweave代码

list_of_orders: {
    order: payload map ((payload01 , indexOfPayload01) -> {
        order_dtl: 
        "" when payload01[30] == "S" 
        otherwise
        "" when payload01[30] == "C"
        otherwise
        [{
            data: some_data
        }],
        order_hdr: {
            data: some_data     
        }
    })
}

此代码将输出以下数据

"list_of_orders": {
    "order": [
        {
            "order_dtl": [
                {
                    "data": "some_data"
                }
            ],
            "order_hdr": {
                    "data": "some_data"
            }
        }
    ]
}

但是只有在payload01 [30]!=“ S”或“ C”时才会这样做 如果payload01 [30]等于“ S”或“ C”,那么它将

"list_of_orders": {
    "order": [
        {
            "order_dtl": "",
            "order_hdr": {
                    "data": "some_data"
            }
        }
    ]
}

之所以这样做,是因为如果order_type不是“ C”或“ S”,则要求我仅包含DETAIL行。

问题在于实际键-order_dtl-仍然存在,我根本不想要任何东西。

如何使KEY有条件?

任何帮助表示赞赏

谢谢

1 个答案:

答案 0 :(得分:0)

您正在寻找的东西称为conditional elements

list_of_orders: {
    order: payload map ((payload01 , indexOfPayload01) -> {
        (order_dtl: 
        [{
            data: some_data
        }]) when((payload01[30] != "S") and (payload01[30] != "C")),
        order_hdr: {
            data: some_data     
        }
    })
}