【问题标题】:How to access a JSON inside an XML in dataweave?如何在 dataweave 中访问 XML 中的 JSON?
【发布时间】:2020-06-04 18:28:16
【问题描述】:

我有一个包含订单数据和订单行项目的 xml 文件。此外,还有一些订单项的信息存储在 XML 中的 JSON 字符串 中,我正在努力正确访问这些信息。

来源如下:

<orders>
    <order order-no="00006640">
        <currency>EUR</currency>
        <product-lineitems>
            <product-lineitem>
                <position>1</position>
                <net-price>22.37</net-price>
            </product-lineitem>
            <product-lineitem>
                <position>2</position>
                <net-price>10.99</net-price>
            </product-lineitem>
        </product-lineitems>
        <custom-attribute attribute-id="return">
            <value>{"status":"RETURNED","position":"2","quantity":"1.000"}</value>
        </custom-attribute>
    </order>
</orders>

目标应如下所示(将 数量 从 JSON 移动到相应的订单项):

<orders>
    <order order-no="00006640">
        <currency>EUR</currency>
        <product-lineitems>
            <product-lineitem>
                <position>1</position>
                <net-price>22.37</net-price>
            </product-lineitem>
            <product-lineitem>
                <position>2</position>
                <net-price>10.99</net-price>
                <quantity>1</quantity>
            </product-lineitem>
        </product-lineitems>
    </order>
</orders>

有没有办法在一次数据编织转换中解决这个问题?

【问题讨论】:

    标签: dataweave anypoint-studio mulesoft


    【解决方案1】:

    可能是这样的..

    %dw 2.0
    output application/xml
    var extractedJson = read(payload.orders.order."custom-attribute".value,"application/json")
    var updProdLineItems = "product-lineitems": payload.orders.order.."product-lineitems" map {
                         "product-lineitem": $.*"product-lineitem" map{
                                         a: $ ++ (if($.position == extractedJson.position) ({"quantity": extractedJson.quantity}) else {})
                         }.a 
                 }
    ---
    {
    orders:
     payload.orders mapObject {
         order @("order-no" : $.@"order-no"): $ - "custom-attribute" - "product-lineitems" ++ updProdLineItems
    
     }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2021-11-22
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多