如何拆分数组中的 objects 以在 Mule4 中生成单个 Objects
How to split the objects in an array to generate as individual Objects in Mule4
我有一个像 below.It 这样的数组请求可以有多个 shipping
Objects 如下所示。在请求中,第二个 objects "shipping"
有两个 Header
和两个 Details
.
此处第一个 Header 需要与第一个详细信息联合创建单独的 Shipping
objects,第二个 Header
需要与第二个 Details
联合创建将 Shipping
Objects 分开,反之亦然,因为它有机会获得更多 headers 和详细信息。
注意:如果 header
存在,则总会有匹配的 details
将以相同的方式存在。
请求和预期响应如下。骡子运行时 4.2.2
任何关于如何重组的想法,对于解决方案的任何想法都将不胜感激。谢谢。
要求:
[
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "61",
"ShippingDate": "10/08/1988",
"Address1": ""
},
"Details": {
"SerialNo": "61",
"LineNo": "1",
"Description": "Shipping Details"
}
}
},
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "21/08/2021",
"Address": "Test1Address"
},
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "22/05/1977",
"Address1": "Test2Address"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test1"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test2"
}
}
}]
预期响应
[
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "61",
"ShippingDate": "10/08/1988",
"Address1": ""
},
"Details": {
"SerialNo": "61",
"LineNo": "1",
"Description": "Shipping Details"
}
}
},
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "21/08/2021",
"Address": "Test1Address"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test1"
}
}
},
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "22/05/1977",
"Address1": "Test2Address"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test2"
}
}
}]
希望我的问题得到解答。让我知道是否需要更多说明。
在这个脚本中,我试图逐个字段逐个逐出 objects 字段。
payload reduce ((shipping, acc=[]) ->
acc ++ (
shipping.Shipping.*Details map ((detail, index) ->
{
Shipping: {
Header: shipping.Shipping.*Header[index],
Details: shipping.Shipping.*Details[index]
}
}
)
)
)
您可以使用 *Details
或 *Header
进行映射,这应该会产生相同的输出
已编辑:缺少 Header 逻辑,运输除了 Header 和详细信息
外没有其他内容
我有一个像 below.It 这样的数组请求可以有多个 shipping
Objects 如下所示。在请求中,第二个 objects "shipping"
有两个 Header
和两个 Details
.
此处第一个 Header 需要与第一个详细信息联合创建单独的 Shipping
objects,第二个 Header
需要与第二个 Details
联合创建将 Shipping
Objects 分开,反之亦然,因为它有机会获得更多 headers 和详细信息。
注意:如果 header
存在,则总会有匹配的 details
将以相同的方式存在。
请求和预期响应如下。骡子运行时 4.2.2
任何关于如何重组的想法,对于解决方案的任何想法都将不胜感激。谢谢。
要求:
[
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "61",
"ShippingDate": "10/08/1988",
"Address1": ""
},
"Details": {
"SerialNo": "61",
"LineNo": "1",
"Description": "Shipping Details"
}
}
},
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "21/08/2021",
"Address": "Test1Address"
},
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "22/05/1977",
"Address1": "Test2Address"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test1"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test2"
}
}
}]
预期响应
[
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "61",
"ShippingDate": "10/08/1988",
"Address1": ""
},
"Details": {
"SerialNo": "61",
"LineNo": "1",
"Description": "Shipping Details"
}
}
},
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "21/08/2021",
"Address": "Test1Address"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test1"
}
}
},
{
"Shipping": {
"Header": {
"Identifier": "100",
"SerialNo": "62",
"ShippingDate": "22/05/1977",
"Address1": "Test2Address"
},
"Details": {
"SerialNo": "62",
"LineNo": "1",
"Description": "Test2"
}
}
}]
希望我的问题得到解答。让我知道是否需要更多说明。
在这个脚本中,我试图逐个字段逐个逐出 objects 字段。
payload reduce ((shipping, acc=[]) ->
acc ++ (
shipping.Shipping.*Details map ((detail, index) ->
{
Shipping: {
Header: shipping.Shipping.*Header[index],
Details: shipping.Shipping.*Details[index]
}
}
)
)
)
您可以使用 *Details
或 *Header
进行映射,这应该会产生相同的输出
已编辑:缺少 Header 逻辑,运输除了 Header 和详细信息
外没有其他内容