0

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"
        }
    ]
}

1 Answers1

2

Hi I think you are using mule 3.x so dw 1.0. Then the solution for your problem is. Use the pluck operator to work over an object as if it were an array of key value pairs.

{
  "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
            })
  }
}
machaval
  • 4,969
  • 14
  • 20