Search code examples
muledataweaveanypoint-studiomulesoft

append array to xml object


i have the following input

{
    "array": [
        {
            "name": "value1"
        },
        {
            "name": "value2"
        },
        {
            "name": "value3"
        }
    ]
}

and would like the following output

<a>
    <b />
    <c>value1</c>   
    <c>value2</c>   
    <c>value3</c>    
</a>

the closest i can get is this

<a>
  <b>
    <c>value1</c>
    <c>value2</c>
    <c>value3</c>
  </b>
</a>

with this dataweave

%dw 2.0
output application/xml writeDeclaration=false
---

a: b: (payload.*array map {c: $.name})

any assistance appreciated thank you

and for extra points .. if anyone can help generate the following with the above sample json payload

<a>
    <b />
    <c>value1</c>
    <d>e</d>    
    <c>value2</c>
    <d>e</d>        
    <c>value3</c>
    <d>e</d>         
</a>

Solution

  • Try with this approach:

    Script

    %dw 2.0
    output application/xml writeDeclaration=false
    ---
    
    a : { 
     b: {}
    }
     ++ {
      (payload.*array map {c: $.name})
     }