使用 Dataweave 将 JSON 输入中多个嵌套数组的字段合并到新的有效负载数组中
Merge fields from multiple nested arrays in JSON input into new payload array using Dataweave
我正在使用 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
}]
}]
}]
}]
}
感谢您的帮助
为了return列表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
}
)
)
并且 returning 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 将数组的嵌套级别展平为一个。
我正在使用 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
}]
}]
}]
}]
}
感谢您的帮助
为了return列表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
}
)
)
并且 returning 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 将数组的嵌套级别展平为一个。