如何在Dataweave 1.0中合并JSON段

时间:2019-05-28 00:49:45

标签: anypoint-studio dataweave mulesoft

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

{
    "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标签,以便我可以看到数据的来源-它将不在最终输出中)

因此,有人要求我使用此逻辑将两个标头合并在一起

  • 从HDR C中获取xxx_cust_long_text_2
  • 从HDR S中获取xxx_cust_decimal_1

将它们合并到一个看起来像这样的单个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"
        }

任何帮助将不胜感激

谢谢

1 个答案:

答案 0 :(得分:0)

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