简体   繁体   English

Dataweave 2.0 中 Excel 到 Json 的映射

[英]Excel to Json Mapping in Dataweave 2.0

I have an excel sheet.我有一个excel表。 I want it to be mapped to a json profile.我希望它被映射到一个 json 配置文件。

sample excel data示例 Excel 数据

I want to convert into json like我想转换成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" : "".....

Like this... the row would be main object and the next row would be the second object ... and next the actual data is to be mapped.像这样......该行将是主要对象,下一行将是第二个对象......接下来将映射实际数据。 I tried but I am unable to get it dynamically.我试过了,但我无法动态获取它。 I used some static index values but wasn't satisfied with the outcome.我使用了一些静态索引值,但对结果不满意。

Any help is appreciated任何帮助表示赞赏

Thank you!谢谢!

Dataweave won't be able to solve it 100% in a dynamic way. Dataweave 将无法以动态方式 100% 解决它。 You may try to use the following expression:您可以尝试使用以下表达式:

%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)
}

You can use another version of the above expression, but instead of considering the start and end column index, you could consider start column index and column count (get 3 columns starting from index 0 instead of get columns from column index 0 to column index 2):您可以使用上述表达式的另一个版本,但不考虑开始和结束列索引,您可以考虑开始列索引和列数(从索引 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM