简体   繁体   中英

Need help in Dataweave Transformation

I have the oracle database table CASH which contains below columns:

REGISTER    DATE    CASE    BAG TYPE
1234    24-SEP-18   1123    112 A
1234    24-SEP-18   1124    113 S
1234    24-SEP-18   1123    116 S
1234    24-SEP-18   1124    117 A
7895    24-SEP-18   2568    119 A
7895    24-SEP-18   2568    118 S

Where the register number are the cash registers which can have multiple CASE linked to it and each CASE can have more than one BAG and Type attached to it. I want to transform it into below XML in Dataweave:

<ROOT>
    <REGISTERS>
        <REGISTER>1234</REGISTER>
        <DATE>24-SEP-2018</DATE>
        <DETAILS>
            <BAG>1123</BAG>
            <DETAIl>
                <BAG>112</BAG>
                <TYPE>A</TYPE>
            </DETAIl>
            <DETAIl>
                <BAG>116</BAG>
                <TYPE>S</TYPE>
            </DETAIl>
        </DETAILS>
        <DETAILS>
            <BAG>1124</BAG>
            <DETAIl>
                <BAG>113</BAG>
                <TYPE>S</TYPE>
            </DETAIl>
            <DETAIl>
                <BAG>117</BAG>
                <TYPE>A</TYPE>
            </DETAIl>
        </DETAILS>
    </REGISTERS>
    <REGISTERS>
        <REGISTER>7895</REGISTER>
        <DATE>24-SEP-2018</DATE>
        <DETAILS>
            <BAG>2568</BAG>
            <DETAIl>
                <BAG>119</BAG>
                <TYPE>A</TYPE>
            </DETAIl>
            <DETAIl>
                <BAG>118</BAG>
                <TYPE>S</TYPE>
            </DETAIl>
        </DETAILS>
    </REGISTERS>
</ROOT>

Could you please give some pointers how can I achieve this in dataweave.

Thanks !!

Assuming you've already read the data from the database, you can use the following:

%dw 1.0
%output application/xml
---
ROOT: payload groupBy (($.REGISTER as :string) ++ ($.DATE as :string)) mapObject ((entries, number) -> {
    REGISTERS: {
        REGISTER: entries[0].REGISTER,
        DATE: entries[0].DATE as :string {format: "yyyy-MM-dd"},
        (entries groupBy $.CASE map DETAILS: {
            CASE: $.CASE[0],
            ($ map DETAIL: {
                BAG: $.BAG,
                TYPE: $.TYPE
            })
        })
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM