Search code examples
muledataweavemule-componentmule4

How to split the objects in an array to generate as individual Objects in Mule4


I have a array request like below.It could have multiple shipping Objects as seen below. In the request, 2nd objects "shipping" having two Header and two Details.

Here 1st Header need to join with 1st Details to create separate Shipping objects and 2nd Header need to join with 2nd Details to create separate Shipping Objects and Vise versa as it having chances to have more headers and details.

Note: If header present there always be matching details will be present in the same fashion.

Request and Expected response are below. Mule Runtime 4.2.2

Any idea how it can be reorganized, any thoughts for the solution will be much appreciated. Thanks.

Request:

[
  {
    "Shipping": {
      "Header": {
        "Identifier": "100",
        "SerialNo": "61",
        "ShippingDate": "10/08/1988",
        "Address1": ""
      },
      "Details": {
        "SerialNo": "61",
        "LineNo": "1",
        "Description": "Shipping Details"
      }
    }
  },
  {
    "Shipping": {
      "Header": {
        "Identifier": "100",
        "SerialNo": "62",
        "ShippingDate": "21/08/2021",
        "Address": "Test1Address"
      },
      "Header": {
        "Identifier": "100",
        "SerialNo": "62",
        "ShippingDate": "22/05/1977",
        "Address1": "Test2Address"
      },
      "Details": {
        "SerialNo": "62",
        "LineNo": "1",
        "Description": "Test1"
      },
      "Details": {
        "SerialNo": "62",
        "LineNo": "1",
        "Description": "Test2"
      }
    }
  }]

Expected Response

[
  {
    "Shipping": {
      "Header": {
        "Identifier": "100",
        "SerialNo": "61",
        "ShippingDate": "10/08/1988",
        "Address1": ""
      },
      "Details": {
        "SerialNo": "61",
        "LineNo": "1",
        "Description": "Shipping Details"
      }
    }
  },
  {
    "Shipping": {
      "Header": {
        "Identifier": "100",
        "SerialNo": "62",
        "ShippingDate": "21/08/2021",
        "Address": "Test1Address"
      },
      "Details": {
        "SerialNo": "62",
        "LineNo": "1",
        "Description": "Test1"
      }
      
    }
  },
  {
    "Shipping": {
      "Header": {
        "Identifier": "100",
        "SerialNo": "62",
        "ShippingDate": "22/05/1977",
        "Address1": "Test2Address"
      },
      "Details": {
        "SerialNo": "62",
        "LineNo": "1",
        "Description": "Test2"
      }
    }
  }]

Hope my question clears. Let me know if requires more clarification.


Solution

  • In this script, I tried to evict re-mapping field by field reusing the original objects.

    payload reduce ((shipping, acc=[]) -> 
        acc ++ (
            shipping.Shipping.*Details map ((detail, index) -> 
                {
                    Shipping: { 
                        Header: shipping.Shipping.*Header[index],
                        Details:  shipping.Shipping.*Details[index] 
                    }
                }
            )
        )
    )
    

    You can map using *Details or *Header which should produce the same output

    EDITED: Header logic was missing and Shipping has no other content than Header and Details