0

I have to do XML to JSON mapping in dataweave. The request XML is:

<Root>
<AccRoot>
    <FinRoot>
        <FinData>
            <SomeValue>Test</SomeValue>
        </FinData>
        <PassRoot>
            <PassData>
                <PassData2>
                    <Leg>001</Leg>
                    <Data>Sample1</Data>
                    <Data2>Sample2</Data2>
                    <Data3>Sample3</Data3>
                </PassData2>
            </PassData>
        </PassRoot>
            <PassData>
                <PassData2>
                    <Leg>002</Leg>
                    <Data>Sample21</Data>
                    <Data2>Sample22</Data2>
                    <Data3>Sample23</Data3>
                </PassData2>
            </PassData>
        <PassRoot>
            <PassData>
                <PassData2>
                    <Leg>003</Leg>
                    <Data>Sample31</Data>
                    <Data2>Sample32</Data2>
                    <Data3>Sample33</Data3>
                </PassData2>
            </PassData>
        </PassRoot>
        <PassRoot>
            <PassData>
                <PassData2>
                    <Leg>004</Leg>
                    <Data>Sample41</Data>
                    <Data2>Sample42</Data2>
                    <Data3>Sample43</Data3>
                </PassData2>
            </PassData>
        </PassRoot>
    </FinEntity>
</PrimeEntity>
</Root>

Resulting JSON should be

Result: Sample1:Sample2:Sample3.Sample21:Sample22:Sample23.Sample31:Sample32:Sample33.Sample41:Sample42:Sample43

i.e. I have to join all the records in 'PassData2' with ':' and then i have to check if 'Leg' with value '+1' is present, if yes then i have to do the same for those elements and join them with '.'

1 Answers1

0
     %dw 1.0
 %output application/json
 ---
 {
     Result: payload.AccRoot.FinRoot..PassData2 
         orderBy ($.Leg as :number) 
         reduce ((val, acc = []) -> 
             (acc ++ [val]) when acc == [] or acc[-1].Leg as :number + 1 == val.Leg as :number 
             otherwise acc
         )
         map ([$.Data, $.Data2, $.Data3] joinBy ':')
         joinBy '.'
 }
  1. Selects all PassData2 nodes

  2. Orders them by Leg (in case they're unordered)

  3. Performs a reduce to ensure the only elements in the array are those with sequential values for Leg

  4. Maps each element left to a concatenation of it's Data* nodes using : as the separator

  5. Joins that final array using .