Excel 到 Json Dataweave 2.0 中的映射

Excel to Json Mapping in Dataweave 2.0

我有一个 excel sheet。我希望将其映射到 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, 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)
}