Here is the sample input 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
],
}
]
]
}
]
*
Here is the expected output (to be able to insert into a db as below):
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
Here is the DW%2.0 code I tried:
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]
}
}
Throwing an error:
(item.videos map(viditem, vidindex)) -> {
^
Invalid input ',', expected ')' for the enclosed expression.
Please see below
%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
})
This will result to:
[
{
"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
}
]
Note here that I have tried to correct your input to make it valid and put in under "data". Idea here is that you will have outer loop (iterating over parent array) through map and have an inner loop (iterating over videos). There might be a more efficient way to combine the two loops into one. I will update if I find something.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments