Excel到Dataweave 2.0中的Json映射

问题描述

我有一张Excel工作表。我希望将其映射到json配置文件。

sample excel data

我想转换成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,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,colCnt) = 
    (item pluck (value,index) -> (key): value) filter ($$ >= startColIdx and (colCnt == -1 or $$ < startColIdx + colCnt)) reduce ($$ ++ $)
---
payload map(item,3),-1)
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...