Skip to content

Instantly share code, notes, and snippets.

@alexandramartinez
Last active May 28, 2020 14:41
Show Gist options
  • Save alexandramartinez/f7037eaf970fb02a04017657cd18b8ad to your computer and use it in GitHub Desktop.
Save alexandramartinez/f7037eaf970fb02a04017657cd18b8ad to your computer and use it in GitHub Desktop.
DW 2.0 functions

DataWeave 2.0 Cheatsheet

Object Concatenation

(object-concatenation.dwl)

  • Using ++
  • Using {( )}
  • Using {([ ])}

String Concatenation

(string-concatenation.dwl)

  • Using ++
  • Using $()

Null Checkers

(null-checkers.dwl)

  • default
  • if/else
  • (key: value) if condition
%dw 2.0
output application/dw
var obj = {
FirstName: "Alexandra",
LastName: "Martinez"
}
fun concat(str1, str2) = (
(str1 default "")
++
(if (str1 != null and str2 != null)
" "
else "")
++
(str2 default "")
)
---
{
(obj),
Name: obj.FirstName concat obj.LastName,
Name1: obj.FirstName concat null,
Name2: null concat obj.LastName,
(Name3: null concat null)
if (concat(null,null) != "")
}
%dw 2.0
output application/json
var object1 = {
"Key1": "Value1",
"Key2": {
"Key3": "Value3"
}
}
var object2 = {
"Key4": "Value4",
"Key5": {
"Key6": "Value6"
}
}
---
// Using ++
//object1 ++ object2
//++(object1, object2)
// Using {( )}
// {
// (object1),
// (object2)
// }
// Using {([ ])}
{([
object1,
object2
])}
%dw 2.0
output application/dw
var str = "World"
var obj = {
text: "World"
}
---
{
opt1: "Hello $(str)!",
opt2: "Hello $(obj.text)!",
opt3: "1+1=$(1+1)",
opt4: "Hello " ++ str ++ "!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment