1

我有一些输出这个 JSON 的 dataweave 1.0 代码。为了清楚起见,我对前缀为“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"
        }

任何帮助,将不胜感激

谢谢

4

1 回答 1

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}
}
于 2019-05-30T22:14:28.067 回答