Mule 4/DW 2.0:在数组对象中迭代数组
Mule 4/DW 2.0: Iterate array inside objects of arrays
这是示例输入 JSON:
*
[
{
"animation_production_studios": [],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
400,
824,
1191
],
},
{
"DASH": true,
"aips": [
401,
825,
1192
],
}
]
]
},
{
"animation_production_studios": ["Studio Chizu"],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
403,
826,
1193
],
},
{
"DASH": true,
"aips": [
404,
827,
1194
],
}
]
]
}
]
*
这是预期的输出(能够插入数据库,如下所示):
animation_production_studios audio videos_dash videos_aips
------------------------------------------------------------------------------------
Null English, Japanese true 400, 824, 1191
Null English, Japanese true 401, 825, 1192
Studio Chizu English, Japanese true 402, 826, 1193
Studio Chizu English, Japanese true 403, 827, 1194
这是我试过的 DW%2.0 代码:
payload map(item, index) -> {
(item.videos map(viditem, vidindex)) -> {
videos_aips: (viditem.videos map ((vitem, vindex) ->
vitem.aips reduce ((vi, vacc) -> vacc + vi )))[0],
show_id: item."show_id",
audio: (payload map ((aitem, aindex) -> item.audio
reduce ((i, acc) -> acc ++ "," ++ i )))[0]
}
}
抛出错误:
(item.videos map(viditem, vidindex)) -> {
^
Invalid input ',', expected ')' for the enclosed expression.
请看下文
%dw 2.0
output application/json
var inputData = read('{
"data": [
{
"animation_production_studios": [],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
400,
824,
1191
]
},
{
"DASH": true,
"aips": [
401,
825,
1192
]
}
]
},
{
"animation_production_studios": [
"Studio Chizu"
],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
403,
826,
1193
]
},
{
"DASH": true,
"aips": [
404,
827,
1194
]
}
]
}
]
}', 'application/json')
fun concatArray(objArray=[], separator=",") =
if (sizeOf(objArray) == 0)
"Null"
else
objArray joinBy separator
---
flatten(inputData.data map(item) ->
item.videos map (vidItem) -> {
"animation_production_studios" : concatArray(item.animation_production_studios, ","),
"audio" : concatArray(item.audio, ","),
"videos_aips" : concatArray(vidItem.aips, ","),
"videos_dash" : vidItem.DASH
})
这将导致:
[
{
"animation_production_studios": "Null",
"audio": "English,Japanese",
"videos_aips": "400,824,1191",
"videos_dash": true
},
{
"animation_production_studios": "Null",
"audio": "English,Japanese",
"videos_aips": "401,825,1192",
"videos_dash": true
},
{
"animation_production_studios": "Studio Chizu",
"audio": "English,Japanese",
"videos_aips": "403,826,1193",
"videos_dash": true
},
{
"animation_production_studios": "Studio Chizu",
"audio": "English,Japanese",
"videos_aips": "404,827,1194",
"videos_dash": true
}
]
请注意,我已尝试更正您的输入以使其有效并放在 "data" 下。这里的想法是,您将通过 map 进行外部循环(遍历父数组)并进行内部循环(遍历视频)。可能有一种更有效的方法将两个循环合二为一。如果我发现了什么,我会更新。
这是示例输入 JSON: *
[
{
"animation_production_studios": [],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
400,
824,
1191
],
},
{
"DASH": true,
"aips": [
401,
825,
1192
],
}
]
]
},
{
"animation_production_studios": ["Studio Chizu"],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
403,
826,
1193
],
},
{
"DASH": true,
"aips": [
404,
827,
1194
],
}
]
]
}
]
*
这是预期的输出(能够插入数据库,如下所示):
animation_production_studios audio videos_dash videos_aips
------------------------------------------------------------------------------------
Null English, Japanese true 400, 824, 1191
Null English, Japanese true 401, 825, 1192
Studio Chizu English, Japanese true 402, 826, 1193
Studio Chizu English, Japanese true 403, 827, 1194
这是我试过的 DW%2.0 代码:
payload map(item, index) -> {
(item.videos map(viditem, vidindex)) -> {
videos_aips: (viditem.videos map ((vitem, vindex) ->
vitem.aips reduce ((vi, vacc) -> vacc + vi )))[0],
show_id: item."show_id",
audio: (payload map ((aitem, aindex) -> item.audio
reduce ((i, acc) -> acc ++ "," ++ i )))[0]
}
}
抛出错误:
(item.videos map(viditem, vidindex)) -> {
^
Invalid input ',', expected ')' for the enclosed expression.
请看下文
%dw 2.0
output application/json
var inputData = read('{
"data": [
{
"animation_production_studios": [],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
400,
824,
1191
]
},
{
"DASH": true,
"aips": [
401,
825,
1192
]
}
]
},
{
"animation_production_studios": [
"Studio Chizu"
],
"audio": [
"English",
"Japanese"
],
"videos": [
{
"DASH": true,
"aips": [
403,
826,
1193
]
},
{
"DASH": true,
"aips": [
404,
827,
1194
]
}
]
}
]
}', 'application/json')
fun concatArray(objArray=[], separator=",") =
if (sizeOf(objArray) == 0)
"Null"
else
objArray joinBy separator
---
flatten(inputData.data map(item) ->
item.videos map (vidItem) -> {
"animation_production_studios" : concatArray(item.animation_production_studios, ","),
"audio" : concatArray(item.audio, ","),
"videos_aips" : concatArray(vidItem.aips, ","),
"videos_dash" : vidItem.DASH
})
这将导致:
[
{
"animation_production_studios": "Null",
"audio": "English,Japanese",
"videos_aips": "400,824,1191",
"videos_dash": true
},
{
"animation_production_studios": "Null",
"audio": "English,Japanese",
"videos_aips": "401,825,1192",
"videos_dash": true
},
{
"animation_production_studios": "Studio Chizu",
"audio": "English,Japanese",
"videos_aips": "403,826,1193",
"videos_dash": true
},
{
"animation_production_studios": "Studio Chizu",
"audio": "English,Japanese",
"videos_aips": "404,827,1194",
"videos_dash": true
}
]
请注意,我已尝试更正您的输入以使其有效并放在 "data" 下。这里的想法是,您将通过 map 进行外部循环(遍历父数组)并进行内部循环(遍历视频)。可能有一种更有效的方法将两个循环合二为一。如果我发现了什么,我会更新。