Search code examples
xmlmuledataweaveanypoint-studio

xml input mapping in dataweave


input file is-

<A>
  <B>
    <merchant_ref>icici</merchant_ref>
    <transaction_tag>sdfhisdb</transaction_tag>
    <transaction_type>inquiry</transaction_type>
    <method>valuelink</method>
    <order_number>123</order_number>
    <amount>1000</amount>
    <currency_code>CAD</currency_code>
  </B>
  <B>
    <merchant_ref>icici</merchant_ref>
    <transaction_tag>sdfhisdb</transaction_tag>
    <transaction_type>inquiry</transaction_type>
    <method>valuelink</method>
    <order_number>123</order_number>
    <amount>2000</amount>
    <currency_code></currency_code>
  </B>
  <B/>
  <B>
    <merchant_ref>icici</merchant_ref>
    <transaction_tag>sdfhisdb</transaction_tag>
    <transaction_type>inquiry</transaction_type>
    <method>valuelink</method>
    <order_number>123</order_number>
    <amount>4000</amount>
    <currency_code></currency_code>
  </B>
  <B>
    <merchant_ref>icici</merchant_ref>
    <transaction_tag>sdfhisdb</transaction_tag>
    <transaction_type>inquiry</transaction_type>
    <method>valuelink</method>
    <order_number>123</order_number>
    <amount>5000</amount>
    <currency_code></currency_code>
  </B>
</A>

tried to map using the below code in dataweave

 %output application/xml
    ---
    Inquiry: payload.A.*B mapObject
        {
        	balanceInquiry:
               { 
               	request: {
                    amount:{
                    amount: payload.A.B.amount/1000 as :number,            
                    currency: payload.A.B.currency_code
                           }
                         }
               }
         }

I am trying to map each value of B which is under A but every time I get the same first thing 5 times as amount 1.0 whether it should change to 2.0 4.0 and 5.0

Please correct me If I am doing something wrong to achieve all the values of B


Solution

  • If using mapObject and not define the parameter (key and value), then you should use its default. The key is defined by default as $$ and the value as $.

    Therefore, the (part of) DataWeave code should be:

    amount: $.amount / 1000 as :number when $.amount != null otherwise 0,
    currency: $.currency_code default ""