0

I have below payload with a hierarchy and trying to filter based on the columnn "DeleteIndicator" = "Y". These are the fixed columns I will be getting. I want to traverse through the payload and check for the DeleteIndicator. Updated the input and expected output for multiple child in inner array.

Input:

    {
   "Num":4363886,
   "LineItems":[
      {
         "DetailGUID":"B439E023360C",
         "DeleteIndicator":"Y"
      },
      {
         "DetailGUID":"B439E023360C",
         "LineQuantity":[
            {
               "AltGUID":"2B43AC4203DC",
               "DeleteIndicator":"Y"
            },
            {
               "AltGUID":"2B43AC4203DD",
               "DeleteIndicator":"Y"
            }
         ]
      },
      {
         "DetailGUID":"B439E023360C",
         "LineQuantity":[
            {
               "AltGUID":"2B43AC4203DC",
               "ShipTo":[
                  {
                     "ShipToGUID":"2B43AC4201AB",
                     "DeleteIndicator":"Y"
                  },
                  {
                     "ShipToGUID":"2B43AC4201AC",
                     "DeleteIndicator":"Y"
                  }
               ]
            }
         ]
      }
   ]
}

Expected Output:

{
   "Num":4363886,
   "Details":[
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":null,
         "ShipToGUID":null
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DC",
         "ShipToGUID":null
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DD",
         "ShipToGUID":null
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DC",
         "ShipToGUID":"2B43AC4201AB"
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DC",
         "ShipToGUID":"2B43AC4201AC"
      }
   ]
}

2 Answers 2

0

This should get your work done.Please let me know if you need any clarifications.

    %dw 2.0
output application/json
---
{
Num:payload.Num,
Details:payload.LineItems map (v0,k0) ->
{
    DetailGUID:v0..DetailGUID[0],
    AltGUID:v0..AltGUID[0],
    ShipToGUID:v0..ShipToGUID[0]
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you. this worked. But I just realized there can be multiple childs at the LineQuantity and ShipTo. In that case how to handle? I have updated my original input and expected output.
0

Well, it is a little complicated, but I have tried my best to make the code readable and documented. Try to look at the problem from a different angle. Think that you are trying to "spread" the fields of "LineItems" across all the "LineQuantity".

%dw 2.0

// This Function takes a json and an array and spreads the json fields
// across each elements of the array.
fun spreadAcrossArray(json: Object, array: Array | Null) = (array default [{}]) map {
    (json),
    ($)
}

//Here is the main logic of getting the payload.
//The idea is first to "Spread" the lineItem Elements across all the "LineQuantity" 
//and then spread those to all "ShipTo" elements. 
fun spreadLineItemAcrossLineQuantity(lineItems: Array) = lineItems map ((lineItem) -> do {
     var lineItemSpreadedAcrossQuantity = 
        {DetailGUID: lineItem.DetailGUID} spreadAcrossArray lineItem.LineQuantity
     ---
     flatten(lineItemSpreadedAcrossQuantity map ({
            DetailGUID: $.DetailGUID,
            AltGUID: $.AltGUID
        } spreadAcrossArray $.ShipTo)) //Map app the lineItemSpreadedAcrossQuantity with spreading them to lineItemSpreadedAcrossQuantity.ShipTo
    }
)

output application/json
---
{
    Num: payload.Num,
    Details: flatten(spreadLineItemAcrossLineQuantity(payload.LineItems))
    map {
        DetailGUID: $.DetailGUID,
        AltGUID: $.AltGUID,
        ShipToGUID: $.ShipToGUID
    }
} 

It requires the array to be flattened later because you are basically passing an Object in the spreadAcrossArray function and in return you are getting an array. And the map function is also returning an array and so you are getting an array of arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.