Я хочу преобразовать 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 и sum, используя приведенный ниже код, ноне получил ожидаемый результат, как показано ниже:
{
"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"
}
]
}