这是样本输入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.
答案 0 :(得分:1)
请参阅下文
%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
}
]
请注意,我已尝试更正您的输入以使其有效,然后将其置于“数据”下。这里的想法是,您将通过map进行外循环(在父数组上迭代),并进行内循环(在视频上迭代)。可能有一种更有效的方法将两个循环组合成一个循环。如果有发现,我会更新。