XML parsing to transform json using dataweave 2

I am trying to parse an XML recursively to create JSON array using dataweave 2 but I am not able to do so.

My Input XML is given below -

<?xml version="1.0" encoding="utf-16"?>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
  <BillsOfMaterials xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/BillsOfMaterials">
    <SenderId>121</SenderId>
    <BOMVersion class="entity">
      <BOMId>BOM0012605</BOMId>
      <ItemId>9650084</ItemId>
      <ItemIdCommercial></ItemIdCommercial>
      <Name>SmartRip for Folding Carton</Name>
      <RecId>5637161103</RecId>
      <BOMTable class="entity">
        <BOMId>BOM0012605</BOMId>
        <Name>SmartRip for Folding Carton</Name>
        <RecId>5637160354</RecId>
        <BOM class="entity">
          <BOMId>BOM0012605</BOMId>
          <BOMQty>1.0000</BOMQty>
          <BOMType>Item</BOMType>
          <ItemId>9650092</ItemId>
          <RecId>5637307757</RecId>
          <UnitId>PCS</UnitId>
          <InventDimBOM class="entity">
            <RecId>5637213290</RecId>
          </InventDimBOM>
          <BOMTable>
            <BOMId>BOM0012613</BOMId>
            <Name>FlexRip Classic Intel kernel and tools</Name>
            <RecId>5637161355</RecId>
            <BOM>
              <BOMType>Item</BOMType>
              <ItemId>9650079</ItemId>
              <BOMQty>1.00</BOMQty>
              <UnitId>PCS</UnitId>
              <BOMId>BOM0012613</BOMId>
              <RecId>5637307799</RecId>
            </BOM>
            <BOM>
              <BOMType>Item</BOMType>
              <ItemId>9644919T</ItemId>
              <BOMQty>1.00</BOMQty>
              <UnitId>PCS</UnitId>
              <BOMId>BOM0012613</BOMId>
              <RecId>5637307800</RecId>
            </BOM>
            <BOM>
              <BOMType>Item</BOMType>
              <ItemId>9644920</ItemId>
              <BOMQty>1.00</BOMQty>
              <UnitId>PCS</UnitId>
              <BOMId>BOM0012613</BOMId>
              <RecId>5637307801</RecId>
            </BOM>
            <BOM>
              <BOMType>Item</BOMType>
              <ItemId>9700720Z</ItemId>
              <BOMQty>1.00</BOMQty>
              <UnitId>PCS</UnitId>
              <BOMId>BOM0012613</BOMId>
              <RecId>5637307802</RecId>
              <BOMTable>
                <BOMId>BOM012173</BOMId>
                <Name></Name>
                <RecId>5637157832</RecId>
                <BOM>
                  <BOMType>Item</BOMType>
                  <ItemId>9700595</ItemId>
                  <BOMQty>1.00</BOMQty>
                  <UnitId>PCS</UnitId>
                  <BOMId>BOM012173</BOMId>
                  <RecId>5637333626</RecId>
                </BOM>
              </BOMTable>
              <BOMTable>
                <BOMId>BOM012173</BOMId>
                <Name></Name>
                <RecId>5637157832</RecId>
                <BOM>
                  <BOMType>Item</BOMType>
                  <ItemId>9700595</ItemId>
                  <BOMQty>1.00</BOMQty>
                  <UnitId>PCS</UnitId>
                  <BOMId>BOM012173</BOMId>
                  <RecId>5637333626</RecId>
                </BOM>
                <BOM>
                  <BOMType>Item</BOMType>
                  <ItemId>9608224Z</ItemId>
                  <BOMQty>1.00</BOMQty>
                  <UnitId>PCS</UnitId>
                  <BOMId>BOM012173</BOMId>
                  <RecId>5637333627</RecId>
                </BOM>       
              </BOMTable>
            </BOM>
            <BOM>
              <BOMType>Item</BOMType>
              <ItemId>9683336L</ItemId>
              <BOMQty>1.00</BOMQty>
              <UnitId>PCS</UnitId>
              <BOMId>BOM0012613</BOMId>
              <RecId>5637307803</RecId>
              <BOMTable>
                <BOMId>BOM0012781</BOMId>
                <Name>Esko Suite 12.1 A5 Software box</Name>
                <RecId>5637165603</RecId>
                <BOM>
                  <BOMType>Item</BOMType>
                  <ItemId>G211249</ItemId>
                  <BOMQty>1.00</BOMQty>
                  <UnitId>PCS</UnitId>
                  <BOMId>BOM0012781</BOMId>
                  <RecId>5637309242</RecId>
                </BOM>
              </BOMTable>
              <BOMTable>
                <BOMId>BOM0012781</BOMId>
                <Name>Esko Suite 12.1 A5 Software box</Name>
                <RecId>5637165603</RecId>
                <BOM>
                  <BOMType>Item</BOMType>
                  <ItemId>G211249</ItemId>
                  <BOMQty>1.00</BOMQty>
                  <UnitId>PCS</UnitId>
                  <BOMId>BOM0012781</BOMId>
                  <RecId>5637309242</RecId>
                </BOM>
                <BOM>
                  <BOMType>Item</BOMType>
                  <ItemId>G25583961_24</ItemId>
                  <BOMQty>1.00</BOMQty>
                  <UnitId>PCS</UnitId>
                  <BOMId>BOM0012781</BOMId>
                  <RecId>5637309243</RecId>
                </BOM>
              </BOMTable>
            </BOM>
          </BOMTable>
        </BOM>
        <BOM class="entity">
          <BOMId>BOM0012605</BOMId>
          <BOMQty>1.0000</BOMQty>
          <BOMType>Item</BOMType>
          <ItemId>9608221Z</ItemId>
          <RecId>5637307758</RecId>
          <UnitId>PCS</UnitId>
          <InventDimBOM class="entity">
            <RecId>5637213290</RecId>
          </InventDimBOM>
        </BOM>
        <BOM class="entity">
          <BOMId>BOM0012605</BOMId>
          <BOMQty>1.0000</BOMQty>
          <BOMType>Item</BOMType>
          <ItemId>9700624Y</ItemId>
          <RecId>5637307759</RecId>
          <UnitId>PCS</UnitId>
          <InventDimBOM class="entity">
            <RecId>5637213291</RecId>
          </InventDimBOM>
          <BOMTable>
            <BOMId>BOM012172</BOMId>
            <Name></Name>
            <RecId>5637157831</RecId>
            <BOM>
              <BOMType>Item</BOMType>
              <ItemId>9644921</ItemId>
              <BOMQty>1.00</BOMQty>
              <UnitId>PCS</UnitId>
              <BOMId>BOM012172</BOMId>
              <RecId>5637333624</RecId>
            </BOM>
          </BOMTable>
        </BOM>
        <BOM class="entity">
          <BOMId>BOM0012605</BOMId>
          <BOMQty>1.0000</BOMQty>
          <BOMType>Item</BOMType>
          <ItemId>9700815</ItemId>
          <RecId>5637307760</RecId>
          <UnitId>PCS</UnitId>
          <InventDimBOM class="entity">
            <RecId>5637213290</RecId>
          </InventDimBOM>
        </BOM>
      </BOMTable>
      <InventDim class="entity">
        <RecId>5637199988</RecId>
      </InventDim>
    </BOMVersion>
  </BillsOfMaterials>
</MessageParts>

I want to create JSON array of all the BOMs (a flat structure with no hierarchy) with parentId as RecId of parent BOM as shown below -

Output -

[
    {
        "id": "5637307757",
        "productId": "9650092",                                                                                                                                 
        "parentId": null
    },
    {
        "id": "5637307799",
        "productId": "9650079",
        "parentId": "5637307757"
    },
    {
        "id": "5637307800",
        "productId": "9644919T",
        "parentId": "5637307757"
    },
    {
        "id": "5637307801",
        "productId": "96AR060W",
        "parentId": "5637307757"
    },
    
    {
        "id": "5637307802",
        "productId": "9700720Z",
        "parentId": "5637307757"
    },
    {
        "id": "5637333626",
        "productId": "9700595",
        "parentId": "5637307802"
    },  
    
    {
        "id": "5637333626",
        "productId": "9700595",
        "parentId": "5637307802"
    },
    {
        "id": "5637333627",
        "productId": "9608224Z",
        "parentId": "5637307802"
    },  
    {
        "id": "5637307803",
        "productId": "9683336L",
        "parentId": "5637307757"
    },
    {
        "id": "5637309242",
        "productId": "G211249",
        "parentId": "5637307803"
    },
    {
        "id": "5637309242",
        "productId": "G211249",
        "parentId": "5637307803"
    },
    {
        "id": "5637309243",
        "productId": "G25583961_24",
        "parentId": "5637307803"
    }
]

Mapping Rules -

  1. id : BOM -> RecId
  2. productId : BOM -> ItemId
  3. parentId : the RecId of the parent BOM

The difference with the suggested answer is that I need to set parentid with the immediate recid of the BOM and not of the BOM table.



from Recent Questions - Stack Overflow https://ift.tt/2EKjIhD
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations