xmljsongroovymulemarkupbuilder

Unable to iterate JSON map using Groovy on Mule payload


I have trouble iterating the JSON (map) in my Groovy script, here's the details to my question.

In Mule I am receiving a JSON. Here it is :

{
    "order": {
        "ContactId": "112",
        "Discount": "0.000000",
        "OrderDate": "2015-03-26 15:26:38",
        "OrderNumber": "VBOKLZZZF",
        "Total": "43.810000",
        "NumberOfChild": "2",
        "PaymentMethod": 1,
        "SpouseName": "Firstname Lastname",
        "Products": [
            {
                "Product": {
                    "Quantity": "1",
                    "UnitPrice": null,
                    "Code": "AB20"
                }
            },
          {
                "Product": {
                    "Quantity": "1",
                    "UnitPrice": null,
                    "Code": "AB20"
                }
            }
        ]
    }
}

I want to convert to XML using Groovy's MarkupBuilder. Here's the output I'm looking for :

<SaveOrder xmlns='http://tempuri.org/'>
  <order xmlns='http://schemas.datacontract.org/2004/07/Service.Entities'>
    <ContactId>112</ContactId>
    <Discount>0.000000</Discount>
    <OrderDate>2015-03-26 15:26:38</OrderDate>
    <OrderNumber>VBOKLZZZF</OrderNumber>
    <Total>43.810000</Total>
    <NumberOfChild>2</NumberOfChild>
    <PaymentMethod>1</PaymentMethod>
    <SpouseName>FirstName Lastname</SpouseName>
    <Products xmlns:"http://schemas.datacontract.org/2004/07/Service.Entities">
      <ns1:Product>
        <ns1:Code>AB20</ns1:Code>
        <ns1:Quantity>1</ns1:Quantity>
      </ns1:Product>
      <ns1:Product>
        <ns1:Code>AB20</ns1:Code>
        <ns1:Quantity>1</ns1:Quantity>
      </ns1:Product>
    </ns1:Products>
  </order>
</SaveOrder>

In Mule I receive the JSON and do an JSON to Object with the return class java.util.Map.

Here's my code for the Groovy Script.

def writer = new StringWriter() 
        def xml = new groovy.xml.MarkupBuilder(writer)
        xml.SaveOrder(xmlns: 'http://tempuri.org/') {
          xml.order(xmlns: 'http://schemas.datacontract.org/2004/07/Service.Entities'){
            ContactId(payload.order.ContactId)
            Discount(payload.order.Discount)
            OrderDate(payload.order.OrderDate)
            OrderNumber(payload.order.OrderNumber)
            Total(payload.order.Total)
            NumberOfChild(payload.order.NumberOfChild)
            PaymentMethod(payload.order.PaymentMethod)
            SpouseName(payload.order.SpouseName)
            xml.Products(){
                for(Product in payload.Products){
                        xml.Product(){
                            Quantity(Product.Quantity)
                            UnitPrice(Product.UnitPrice)
                            Code(Product.Code)
                        }

                }
            }
          }
        } 
        result = writer.toString() 

But I'm only able to get this

<SaveOrder xmlns='http://tempuri.org/'>
  <order xmlns='http://schemas.datacontract.org/2004/07/Service.Entities'>
    <ContactId>112</ContactPrestashopId>
    <Discount>0.000000</Discount>
    <OrderDate>2015-03-26 15:26:38</OrderDate>
    <OrderNumber>VBOKLZZZF</OrderNumber>
    <Total>43.810000</Total>
    <NumberOfChild>2</NumberOfChild>
    <PaymentMethod>1</PaymentMethod>
    <SpouseName>Firstname Lastname</SpouseName>
    <Products />
  </order>
</SaveOrder>

Can someone help me with the iteration in the Groovy Script?

Thanks for helping


Solution

  • You're not getting to the Products properly, it's payload.order.Products;

    def xml = new StringWriter().with { w ->
        new MarkupBuilder(w).with {
            SaveOrder(xmlns: 'http://tempuri.org/') {
                order(xmlns: 'http://schemas.datacontract.org/2004/07/Service.Entities') {
                    ContactId(payload.order.ContactId)
                    Discount(payload.order.Discount)
                    OrderDate(payload.order.OrderDate)
                    OrderNumber(payload.order.OrderNumber)
                    Total(payload.order.Total)
                    NumberOfChild(payload.order.NumberOfChild)
                    PaymentMethod(payload.order.PaymentMethod)
                    SpouseName(payload.order.SpouseName)
                    Products(xmlns:"http://schemas.datacontract.org/2004/07/Service.Entities") {
                        payload.order.Products.each { p ->
                            'ns1:Product' {
                                'ns1:Code'(p.Product.Code)
                                'ns1:Quantity'(p.Product.Quantity)
                            }
                        }
                    }
                }
            }
        }
        w.toString()
    }