如何仅在值满足特定条件时包含键

How to only include a key if the value meets a certain criteria

我有这段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 成为条件?

感谢任何帮助

谢谢

你正在寻找的东西叫做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     
        }
    })
}