Advanced Composition 2

Calculation & Aggregation

Advanced Composition 2

Task

Build a "flattened" array of all items regardless of whether they are a main, sub, or sub-sub item. The order of output items should be "depth-first," meaning the array needs to show the first main line item followed by all of its sub-items and sub-sub items before the second main line item appears.

Hint: This requires understanding array functions

Your Expression
Your Result
No data
Input JSON
{
    line_items:[
      {
        product_id:1,
        sub_line_items:[
          {
            product_id:3,
            quantity:5,
            unit_price:10,
            sub_sub_line_items:[
              {
                product_id:6,
                quantity:2,
                unit_price:5
              }
            ]
          }
        ]
      },
      {
        product_id:2,
        quantity:53,
        unit_price:10.876,
        sub_line_items:[
          {
            product_id:4,
            quantity:3,
            unit_price:10.99,
            sub_sub_line_items:[
              {
                product_id:7,
                quantity:1,
                unit_price:2.5
              }
            ]
          },
          {
            product_id:5,
            quantity:9,
            unit_price:44.77
          }
        ]
      }
    ]
}
Expected Output
[
    {
      product_id:1,
      type:"MAIN_ITEM"
    },
    {
      product_id:3,
      type:"SUB_ITEM"
    },
    {
      product_id:6,
      type:"SUB_SUB_ITEM"
    },
    {
      product_id:2,
      type:"MAIN_ITEM"
    },
    {
      product_id:4,
      type:"SUB_ITEM"
    },
    {
      product_id:7,
      type:"SUB_SUB_ITEM"
    },
    {
      product_id:5,
      type:"SUB_ITEM"
    }
]