我有以下数据:
{
"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": []
}
]
}
]
}
注意:orderLines数组可以是任意深度
我想要这样的输出:
{
"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": []
}
]
}
答案 0 :(得分:2)
您可以使用执行以下操作的递归函数来做到这一点:
[]
orderLines
(无子字段)orderLines
像这样:
%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)
}