Reputation: 25
I have the below data:
{
"currencyCode": "GBP",
"type": "New",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8pQAC",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8qQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8rQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
}
]
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8vQAC",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8wQAC",
"parentId": "8023O000000Cc8vQAC",
"orderLines": []
}
]
}
]
}
Note: orderLines array could be of any depth
I want to have output like this:
{
"currencyCode": "GBP",
"type": "New",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8qQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8rQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8vQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8wQAC",
"parentId": "8023O000000Cc8vQAC",
"orderLines": []
}
]
}
Upvotes: 1
Views: 332
Reputation: 1538
You can do it with a recursive function that does the following:
[]
orderLines
of each level (without the children field) orderLines
of the next level like this:
%dw 2.0
output application/json
fun getNextLevel(orders: Array) = flatten(orders.orderLines)
fun removeChildren(orders: Array) =
orders map ((value) ->
value - "orderLines"
)
fun getOrders(orders: Array) = do {
orders match {
case [] -> []
else -> removeChildren(orders) ++ getOrders(getNextLevel(orders))
}
}
---
{
"currencyCode": payload.currencyCode,
"type": payload."type",
"orderLines": getOrders(payload.orderLines)
}
Upvotes: 3