0

我想将 XML 转换为 JSON 下面是我的详细信息和要求。

我有以下 XML:

    <Message>
    <OrderList>
        <Order>
            <PaymentMethods>
                <PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="1111" CreditCardType="VISA" AmountToRefund="10" />
                <PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="2222" CreditCardType="MASTER" AmountToRefund="20" />
            </PaymentMethods>
        </Order>
        <Order>
            <PaymentMethods>
                <PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="1111" CreditCardType="VISA" AmountToRefund="10" />
                <PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="2222" CreditCardType="MASTER" AmountToRefund="20" />
                <PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="3333" CreditCardType="VISA" AmountToRefund="12" />
            </PaymentMethods>
        </Order>
    </OrderList>
</Message>

我在下面尝试使用 groupBy 并使用下面的代码求和,但没有得到预期的输出,如下所示:

    {
    "template_attributes": {
        "payment_methods": payload.Message.OrderList..*PaymentMethod groupBy ($.CreditCardType ++ $.CreditCardNo) map ((paymentMethod, index) -> {
            "first_name": paymentMethod.@FirstName,
            "last_name": paymentMethod.@LastName,
            "card_type": paymentMethod.@CreditCardType,
            "card_no": paymentMethod.@CreditCardNo,
            "amount_to_refund": paymentMethod.@AmountToRefund,
            "payment_type": sum paymentMethod.@PaymentType

        })
        }
}

我想要下面的输出,所有的支付方式都应该基于 card_type 和 credit_card_no 进行组合:

 template_attributes:{
    payment_methods:[
        {
            first_name: "Avnish",
            last_name: "Singh",
            credit_card_no: "1111",
            card_type: "VISA",
            amount: "20"
        },
        {
            first_name: "Avnish",
            last_name: "Singh",
            credit_card_no: "2222",
            card_type: "MASTER",
            amount: "40"
        },
        {
            first_name: "Avnish",
            last_name: "Singh",
            credit_card_no: "3333",
            card_type: "VISA",
            amount: "12"
        }
    ]
}
4

1 回答 1

1

嗨,我认为您使用的是 mule 3.x 所以 dw 1.0。那么你的问题的解决方案就是。使用 pluck 操作符来处理一个对象,就像它是一个键值对数组一样。

{
  "template_attributes": {
    "payment_methods": 
        payload.Message.OrderList..*PaymentMethod 
            groupBy ((value) -> value.@CreditCardType ++ value.@CreditCardNo)             
            pluck ((paymentMethod, key) -> {
                "first_name": paymentMethod[0].@FirstName,
                "last_name": paymentMethod[0].@LastName,
                "card_type": paymentMethod[0].@CreditCardType,
                "card_no": paymentMethod[0].@CreditCardNo,
                "amount_to_refund": sum paymentMethod.@AmountToRefund,
                "payment_type": paymentMethod[0].@PaymentType
            })
  }
}
于 2019-05-27T14:02:11.197 回答