I am using Mule 4.3 and Dataweave 2.x From database am getting following records example:
[
{
"gp": "G1",
"parent": "P1",
"child": "C1"
},
{
"gp": "G1",
"parent": "P1",
"child": "C2"
},
{
"gp": "G2",
"parent": "P1",
"child": "C3"
},
{
"gp": "G1",
"parent": "G1",
"child": "C4"
}
]
Each element in array represents a row of data and each row has hierarchical data (Grandparent , Parent and Child data)
I need to generate the output in XML as below:
<list>
<genealogy>
<code>G1</code>
<hierarchy>
<!-- here parent is not included in hierarchy as it has same value of grandparent -->
<genealogy>
<code>C4</code>
<hierarchy/>
</genealogy>
<!-- here 2 instances of P1 have become One value -- >
<genealogy>
<code>P1</code>
<hierarchy>
<genealogy>
<code>C1</code>
<hierarchy/>
</genealogy>
<genealogy>
<code>C2</code>
<hierarchy/>
</genealogy>
</hierarchy>
</genealogy>
</hierarchy>
</genealogy>
<genealogy>
<code>G2</code>
<hierarchy>
<genealogy>
<code>P1</code>
<hierarchy>
<genealogy>
<code>C3</code>
<hierarchy/>
</genealogy>
</hierarchy>
</genealogy>
</hierarchy>
</genealogy>
</list>
Here couple of challenges are:
- Its a 3 level Hierarchy where Grandparent needs to be the first element , within which we have the Parent(s) and within Parent Child(ren)
- If the parent ( Ex : G1 last object in input array ) has same value of grandparent then parent should be dropped in hierarchy ( Grandparent and then directly Child )
I am stumped how to do the needful in Dataweave and have tried things like groupBy , pluck etc but am not able to get desired result