I want to transform XML to JSON below are my details and requirement.
I have below 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>
I have tried below by using groupBy and sum using below code but didn't get expected output, as shown in below:
{
"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
})
}
}
I want below Output, where all the payment methods should be combined based on the card_type and 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"
}
]
}