简体   繁体   中英

DataWeave 1.0 - Mulesoft query

I am new to dataweave language. Could you please help to solve this syntax error "Invalid input "otherwise (", expected is, *, <=, <, >=, as, or,

, ~=, -, functionCall, ==, fullAttributes, +, !=, :, and, / or >" at first occurence of word "otherwise' below.

%dw 1.0
%output application/json
---
{
ref:[{
    captureDetails : {
            captureSource : "aa",
            captureDate : ""
            },
    effectiveDate : "",
    expiryDate : "",
    preferenceLevel : "customer",
    sourcePreferenceCode: { 
       (( preferenceCode: "A1" ) when trim payload.key == "REF1" 
         otherwise ( 
                   ( preferenceCode: "A2" ) when trim payload.key == "REF2"
          otherwise (
                    ( preferenceCode: "A3" ) when trim payload.key == "REF3"
            otherwise (
                    ( preferenceCode: "A4" ) when trim payload.key == "REF4"
                otherwise (
                    ( preferenceCode: "A5" ) when trim payload.key == "REF5"
                    otherwise (
                        ( preferenceCode: "A6" ) when trim payload.key == "REF6"
                        otherwise (
                            ( preferenceCode: "A7" ) when trim payload.key == "REF7"
                            otherwise (
                                ( preferenceCode: "A8" ) when trim payload.key == "REF8"                                    
                                otherwise ( preferenceCode: "" )
                            )                               
                        )
                    )                       
                )                   
            )               
          )     
        )
      ),                                                                     
     preferenceValue: trim payload.value
    },  
}],
lastUpdateDetails:{
lastUpdateId:"adam",
lastUpdateTimestamp:"2019-07-19",
lastUpdateFunction:"U",
lastUpdateChannel : "P"
}
}

Not sure why you have so many parentheses. This should work:

%dw 1.0
%output application/json
---
{
    ref:[{
        captureDetails : {
            captureSource : "aa",
            captureDate : ""
        },
        effectiveDate : "",
        expiryDate : "",
        preferenceLevel : "customer",
        sourcePreferenceCode: { 
            preferenceCode: "A1" when trim payload.key == "REF1" 
                otherwise preferenceCode: "A2" when trim payload.key == "REF2"
                otherwise preferenceCode: "A3" when trim payload.key == "REF3"
                otherwise preferenceCode: "A4" when trim payload.key == "REF4"
                otherwise preferenceCode: "A5" when trim payload.key == "REF5"
                otherwise preferenceCode: "A6" when trim payload.key == "REF6"
                otherwise preferenceCode: "A7" when trim payload.key == "REF7"
                otherwise preferenceCode: "A8" when trim payload.key == "REF8"
                otherwise preferenceCode: "",
            preferenceValue: trim payload.value
        }
    }],
    lastUpdateDetails:{
        lastUpdateId:"adam",
        lastUpdateTimestamp:"2019-07-19",
        lastUpdateFunction:"U",
        lastUpdateChannel : "P"
    }
}

I would use the match operator which should be clearer for this case:

%dw 1.0
%output application/json
---
{
    ref:[{
        captureDetails : {
            captureSource : "aa",
            captureDate : ""
        },
        effectiveDate : "",
        expiryDate : "",
        preferenceLevel : "customer",
        sourcePreferenceCode: { 
            preferenceCode: (trim payload.key) match {
                "REF1" -> "A1",
                "REF2" -> "A2",
                "REF3" -> "A3",
                "REF4" -> "A4",
                "REF5" -> "A5",
                "REF6" -> "A6",
                "REF7" -> "A7",
                "REF8" -> "A8",
                default -> ""
            },
            preferenceValue: trim payload.value
        }
    }],
    lastUpdateDetails:{
        lastUpdateId:"adam",
        lastUpdateTimestamp:"2019-07-19",
        lastUpdateFunction:"U",
        lastUpdateChannel : "P"
    }
}

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