简体   繁体   中英

Excel to Json Mapping in Dataweave 2.0

I have an excel sheet. I want it to be mapped to a json profile.

sample excel data

I want to convert into json like

[
  {
    "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. 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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