如何在dataweave中访问XML中的JSON?

问题描述 投票:0回答:1

我有一个包含订单数据和订单行项目的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转换中解决这个问题?

dataweave anypoint-studio mulesoft
1个回答
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

 }
}

© www.soinside.com 2019 - 2024. All rights reserved.