Vik*_*rma 2 dataweave mulesoft mule4
我有 50,000 多个 json 对象埋在嵌套数组中。我想把它们拉出来,这样它们就可以在单个数组中。而且这些嵌套数组是相当随机的,没有模式。例如
[ [ [ [ [ {"a":1 } ], [ {"b":2 } ] ], [[{"c":3 }]] ] ], [{"d":4 }] ]
必须转换为
[{"a":1},{"b":2},{"c":3},{"d":4}]
使用 Dataweave 2.0
使用过展平,但看起来它的功能不正确。
这是使用递归函数执行此操作的一种方法:
%dw 2.0
output application/json
fun flattenAllLevels(arr: Array) = do {
arr reduce ((item, acc = []) ->
item match {
case x is Array -> acc ++ flattenAllLevels(x)
else -> acc << item
}
)
}
---
flattenAllLevels(payload)
Run Code Online (Sandbox Code Playgroud)