Avnish Kumar Singh
Avnish Kumar Singh

Reputation: 23

In dataweave converting multi line list of list in XML to single array in json

In dataweave, I want to transform the below complex xml to json output as shown below:

<Message>
    <Shipment>
        <Containers>
            <Container TrackingNo="Abc1" />
         </Containers>
    </Shipment>
    <Shipment>
        <Containers>
            <Container TrackingNo="Abc2" />
            <Container TrackingNo="Abc3" />
        </Containers>
    </Shipment>
    <Shipment>
        <Containers>
            <Container TrackingNo="Abc4" />
            <Container TrackingNo="Abc5" />
            <Container TrackingNo="Abc6" />
        </Containers>
    </Shipment>
</Message>

I want to get the output in below JSON format after transformation in dataweave:

tracking_no:[
    {
        TrackingNo="Abc1"
    },
    {
        TrackingNo="Abc2"
    },
    {
        TrackingNo="Abc3"
    },
    {
        TrackingNo="Abc4"
    },
    {
        TrackingNo="Abc5"
    },
    {
        TrackingNo="Abc6"
    }
]

Upvotes: 1

Views: 314

Answers (1)

machaval
machaval

Reputation: 5059

You can use the descendant selector and then use the attributes selectors

%dw 2.0
output application/json
---
tracking_no: payload..*Container.@

Upvotes: 5

Related Questions