我正在使用Anypoint Studio 7.3和Mule 4.2。
使用下面的JSON输入,我想创建新的有效负载以写入数据库表,其中包括一个有效负载,该有效负载从数组中的每个记录条目构建客户列表以创建如下所示的列表条目:
[{
recordId,
customerId
}]
,对于交易列表也是如此,每个列表条目如下所示:
[{
recordId
customerId
transactionId
}]
但是当我尝试转换数据时,字段值显示为null或列表,而不是对象中的单个字段,如下所示:
{
"customers": [{
"record": "1234",
"customerId": [
"5435e1cd-146d-4aac-9164-4a2d80d5eccd"
]
}]
}
代替此:
{
"customers": [{
"recordId": "1234",
"customerId": "5435e1cd-146d-4aac-9164-4a2d80d5eccd"
}]
}
JSON输入:
{
"records": [{
"recordId": "1234",
"customers": [{
"customerId": "1234",
"transactions": [{
"transactionId": "1234",
"prices": [{
"priceId": "1234",
"price": 1.00
}]
}]
}]
}]
}
感谢您的帮助
答案 0 :(得分:3)
为了返回customers from each record
的列表
%dw 2.0
output application/java
---
payload.records flatMap
((record, index) ->
record.customers map ((customer, index) ->
{
recordId: record.recordId,
customerId: customer.customerId
}
)
)
对于返回list of transactions with each list entry
%dw 2.0
output application/json
---
payload.records flatMap
((record, index) ->
record.customers flatMap ((customer, index) ->
customer.transactions map ((transaction, index) ->
{
recordId: record.recordId,
customerId: customer.customerId,
transactionId: transaction.transactionId
}
)
)
)
此处的关键部分是使用flatMap将数组的嵌套级别展平为一个。