簡體   English   中英

Dataweave 2.0 中 Excel 到 Json 的映射

[英]Excel to Json Mapping in Dataweave 2.0

我有一個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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM