我想将以下输入传输为输出 JSON 格式。
并且密钥应该是动态的。我还有其他具有独特 JSON 结构的有效负载。
我该如何解决这个问题?
输入:
{"Report_Entry": [{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Lines": [{
"Project": "Proj1"
},{
"Project": "Proj2"
}],
"Amendments": [{
"Name": "Amd 1",
"Status": "Approved"
},{
"Name": "Amd 2",
"Status": "Approved"
}]
}]
}
输出:
[{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj1",
"Name": "Amd 1",
"Status": "Approved"
},{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj1",
"Name": "Amd 2",
"Status": "Approved"
},{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj2",
"Name": "Amd 1",
"Status": "Approved"
},{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj2",
"Name": "Amd 2",
"Status": "Approved"
}]
我假设 Report_Entry 中只有一个元素。在线和修改上嵌套地图将为您提供所需元素的组合。由于嵌套映射返回嵌套数组,因此顶部需要
flatMap()
或 flatten()
来展平两个映射的结果。
%dw 2.0
output application/json
var root=payload.Report_Entry[0]
---
root.Lines
flatMap ((line, line_index) ->
root.Amendments map ((amendment, amendment_index) ->
{
Accepted_Currencies: root.Accepted_Currencies,
Amount: root.Amount
}
++ line
++ amendment )
)
输出:
[
{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj1",
"Name": "Amd 1",
"Status": "Approved"
},
{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj1",
"Name": "Amd 2",
"Status": "Approved"
},
{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj2",
"Name": "Amd 1",
"Status": "Approved"
},
{
"Accepted_Currencies": "USD; EUR",
"Amount": "22",
"Project": "Proj2",
"Name": "Amd 2",
"Status": "Approved"
}
]