Needs 2 Selects 😉

Data (Compose)
[
{
"Qty": "225",
"_id": "659921c7d8df6256c4365004",
"item": "6599221f4069ad0deeffc462"
},
{
"Qty": "2100",
"_id": "659921c7d8df6256c4365004",
"item": "6599221f4069ad0deeffc461"
}
]
DataEnhanced (Select)
From
outputs('Data')
Map
addProperty(
item(),
'variantsString',
concat(
'{"variant":"', item()['item'], '","Qty":"', item()['Qty'], '"}'
)
)
Select
From
union(
xpath(
xml(json(concat('{"Root":{"Item":', body('DataEnhanced'),'}}'))),
'//_id/text()'
),
json('[]')
)
Map to
item()
Map variants
json(
concat(
'[',
join(
xpath(
xml(json(concat('{"Root":{"Item":', body('DataEnhanced'),'}}'))),
concat('//Item[_id="', item(), '"]/variantsString/text()')
),
','
),
']'
)
)
Result
[
{
"to": "659921c7d8df6256c4365004",
"variants": [
{
"variant": "6599221f4069ad0deeffc462",
"Qty": "225"
},
{
"variant": "6599221f4069ad0deeffc461",
"Qty": "2100"
}
]
}
]