Dataweave 2.0中的Excel到Json映射



我有一张excel表。我希望它被映射到一个json配置文件。

样本excel数据

我想转换成类似的json

[
{
"Scope" : {
"Content owner" : "",
"Language" : "",
"Territory" : ""
},
"Title" : {
"Content ID" : "",
"Billing ID" : "",
"IMDB" : "",
"Name" : "",
"Episode Number" : "",
"Episode Sequence" : "",
"Container Position" : "",
"Run Length" : "",
"Work Type" : "",
"Short Synopsis" : "",
"Long Synopsis" : "",
"Original Language" : "",
"Rating Set1" : "",
"Rating Set2" : "",
"Rating Set3" : "",
"Rating Set4" : "",
"Rating Set5" : "".....

像这样。。。该行将是主对象,下一行将是第二个对象。。。并且接下来实际数据将被映射。我试过了,但无法动态获取。我使用了一些静态索引值,但对结果不满意。

感谢提供的任何帮助

谢谢!

Dataweave无法以动态方式100%解决此问题。您可以尝试使用以下表达式:

%dw 2.0
output application/json
//endIndex: use -1 to include all remaining fields
fun addFields(item, startColIdx, endColIdx) = 
(item pluck (value, key, index) -> (key): value) filter ($$ >= startColIdx and (endColIdx == -1 or $$ <= endColIdx)) reduce ($$ ++ $)
---
payload map(item, index) -> {
'Scope': addFields(item, 0, 2),
'Title': addFields(item, 3, -1)
}

您可以使用上面表达式的另一个版本,但您可以考虑起始列索引和列计数,而不是考虑起始列和结束列索引(从索引0开始获取3列,而不是从列索引0到列索引2获取列(:

%dw 2.0
output application/json
//endIndex: use -1 to include all remaining columns
fun addFields(item, startColIdx, colCnt) = 
(item pluck (value, key, index) -> (key): value) filter ($$ >= startColIdx and (colCnt == -1 or $$ < startColIdx + colCnt)) reduce ($$ ++ $)
---
payload map(item, index) -> {
'Scope': addFields(item, 0, 3),
'Title': addFields(item, 3, -1)
}

最新更新