如何在 Dataweave 1.0 中合并 JSON 段

How to merge JSON segments in Dataweave 1.0

我有一些输出此 JSON 的 dataweave 1.0 代码。 我对 4 个字段特别感兴趣,为清楚起见,我在前面加上 "xxx_"

{
    "list_of_orders": {
        "order": [{
                "order_hdr": {
                    "HDR": "C",
                    "xxx_cust_long_text_2": "FREIGHT CHARGE APPLIES",
                    "customer_po_nbr": "55555",
                    "order_nbr": "99999",
                    "dest_dept_nbr": "12345",
                    "shipto_name": "BOB",
                    "shipto_addr3": "",
                    "shipto_addr": "VICTORIA STREET",
                    "shipto_addr2": "",
                    "shipto_city": "HAMILTON",
                    "cust_nbr": "13245",
                    "cust_name": "ROB",
                    "cust_addr3": "",
                    "cust_addr": "PO BOX 11111",
                    "cust_addr2": "MANUKAU CITY",
                    "cust_city": "AUCKLAND",
                    "ord_date": "2019-05-02",
                    "ship_via": "",
                    "xxx_cust_decimal_1": "",
                    "cust_decimal_3": 205.00,
                    "cust_decimal_4": 30.75,
                    "cust_decimal_5": 235.75,
                    "customer_po_type": "Y",
                    "facility_code": null,
                    "start_ship_date": "",
                    "stop_ship_date": "",
                    "company_code": null,
                    "order_type": "SBS",
                    "action_code": "CREATE"
                }
            },
            {
                "order_hdr": {
                    "HDR": "S",
                    "xxx_cust_long_text_2": "",
                    "customer_po_nbr": "55555",
                    "order_nbr": "99999",
                    "dest_dept_nbr": "12345",
                    "shipto_name": "BOB",
                    "shipto_addr3": "",
                    "shipto_addr": "VICTORIA STREET",
                    "shipto_addr2": "",
                    "shipto_city": "HAMILTON",
                    "cust_nbr": "13245",
                    "cust_name": "ROB",
                    "cust_addr3": "",
                    "cust_addr": "PO BOX 11111",
                    "cust_addr2": "MANUKAU CITY",
                    "cust_city": "AUCKLAND",
                    "ord_date": "2019-05-02",
                    "ship_via": "",
                    "xxx_cust_decimal_1": "10.00",
                    "cust_decimal_3": 205.00,
                    "cust_decimal_4": 30.75,
                    "cust_decimal_5": 235.75,
                    "customer_po_type": "Y",
                    "facility_code": null,
                    "start_ship_date": "",
                    "stop_ship_date": "",
                    "company_code": null,
                    "order_type": "SBS",
                    "action_code": "CREATE"
                }
            }
        ]
    }
}

(请注意,HDR 标签只是添加在这里,以便我可以看到数据的来源——它不会出现在最终输出中)

所以我被要求使用这个逻辑将两个 headers 合并在一起[=13​​=]

将它们合并成一个 order_hdr,看起来像这样

        "order_hdr": {
          "xxx_cust_long_text_2": "FREIGHT CHARGE APPLIES",
          "customer_po_nbr": "55555",
          "order_nbr": "99999",
          "dest_dept_nbr": "12345",
          "shipto_name": "BOB",
          "shipto_addr3": "",
          "shipto_addr": "VICTORIA STREET",
          "shipto_addr2": "",
          "shipto_city": "HAMILTON",
          "cust_nbr": "13245",
          "cust_name": "ROB",
          "cust_addr3": "",
          "cust_addr": "PO BOX 11111",
          "cust_addr2": "MANUKAU CITY",
          "cust_city": "AUCKLAND",
          "ord_date": "2019-05-02",
          "ship_via": "",
          "xxx_cust_decimal_1": "10.00",
          "cust_decimal_3": 205.00,
          "cust_decimal_4": 30.75,
          "cust_decimal_5": 235.75,
          "customer_po_type": "Y",
          "facility_code": null,
          "start_ship_date": "",
          "stop_ship_date": "",
          "company_code": null,
          "order_type": "SBS",
          "action_code": "CREATE"
        }

如有任何帮助,我们将不胜感激

谢谢

%dw 1.0
%output application/json
%var headerS = payload.list_of_orders.order.order_hdr filter ($.HDR == "S") 
%var headerC  = payload.list_of_orders.order.order_hdr filter ($.HDR == "C") 
---
{
    header: payload.list_of_orders.order[0].order_hdr
        - "HDR"
        - "xxx_cust_decimal_1"
        - "xxx_cust_long_text_2"
        ++ { xxx_cust_decimal_1: headerS[0].xxx_cust_decimal_1}
        ++ { xxx_cust_long_text_2: headerC[0].xxx_cust_long_text_2}
}