Contents
Overview
Quick Start
Prerequisites
* Studio 7
Setup a Project
* File > New > Mule Project
– Project Name: testscript
* Drag Transform Message to canvas
Concatenate String
* Script
%dw 2.0
output application/json
---
{
myString: ("hello" ++ " world")
}* Output
{
"myString": "hello world"
}JSON to XML
* Script
%dw 2.0
output application/xml
---
{
"myString": ("hello" ++ " world")
}* Output
<!--?xml version='1.0' encoding='windows-1252'?--> <mystring>hello world</mystring>
Supported Data Types
* Script
%dw 2.0
output application/json
---
{
/*
* A multi-line
* comment here.
*/
myString: "hello world",
myNumber: 123,
myFloatingPointNumber: 123.456,
myVeryBigNumber: 12341234134123412341234123,
myDate: |2018-12-07|,
myTime: |11:55:56|,
myDateTime: |2018-10-01T23:57:59-03:00|,
myBoolean: true,
myArray: [ 1, 2, 3, 5, 8],
myMixedArray: [ 1, 2, "blah", { hello: "there" } ],
myObjectKeyValuePair: { innerKey: "innerValue" },
myObjectWithConditionalField: { a : { b : 1, ( c : 2 ) if true, (d : 4) if false } },
myNull: null,
myBinary: "abcd1234123" as Binary
//A one-line comment here.
}* Output
{
"myString": "hello world",
"myNumber": 123,
"myFloatingPointNumber": 123.456,
"myVeryBigNumber": 12341234134123412341234123,
"myDate": "2018-12-07",
"myTime": "11:55:56",
"myDateTime": "2018-10-01T23:57:59-03:00",
"myBoolean": true,
"myArray": [
1,
2,
3,
5,
8
],
"myMixedArray": [
1,
2,
"blah",
{
"hello": "there"
}
],
"myObjectKeyValuePair": {
"innerKey": "innerValue"
},
"myObjectWithConditionalField": {
"a": {
"b": 1,
"c": 2
}
},
"myNull": null,
"myBinary": "abcd1234123"
}Define and Use Var as Input
* Script
%dw 2.0
var myjson = {"hello": "world"}
output application/json
---
myjson* Output
{
"hello": "world"
}Use Function
* Script
%dw 2.0
var myjson = {
"a": avg([1,1000]),
"b": avg([1,2,3])
}
output application/json
---
myjson* Output
{
"a": 500.5,
"b": 2.0
}Read from an Input
* Script
%dw 2.0
var myRead = read("<car><color>Read</color></car>", "application/xml")
output application/json
---
{
mySelection: myRead.car
}* Output
{
"mySelection": {
"color": "Read"
}
}Read From a File
* Script
%dw 2.0
output application/json
---
readUrl("classpath://myJason.json", "application/json")* Output
{
"hello": "world"
}map Function
* Script
%dw 2.0
output application/json
---
{
( //Need this to evaluate
["a","b","c"] map ((value, index) -> {
(index): value
})
)
}* Output
{
"0": "a",
"1": "b",
"2": "c"
}pluck Function
* Script
%dw 2.0
output application/json
---
{
"0": "a",
"1": "b",
"2": "c"
} pluck ((value) -> value)* Output
[ "a", "b", "c" ]
More Complex
* Script
%dw 2.0
var myVar = [
{ bookId: 101,
title: "world history",
price: "19.99"
},
{
bookId: 202,
title: 'the great outdoors',
price: "15.99"
}
]
var myVar2 = [
{
bookId: 101,
author: "john doe"
},
{
bookId: 202,
author: "jane doe"
}
]
output application/json
---
myVar map (item, index) -> using (id = item.bookId) {
"id" : id,
"topic" : item.title,
"cost" : item.price as Number,
(myVar2 filter ($.*bookId contains id) map (item) -> {
author : item.author
})
}* Output
[
{
"id": 101,
"topic": "world history",
"cost": 19.99,
"author": "john doe"
},
{
"id": 202,
"topic": "the great outdoors",
"cost": 15.99,
"author": "jane doe"
}
]Operators
Relational/Equality Operators
== ~= < > <= >=
Logical Operators
not # (not true or true) is same as (not(true or true)) which evaluates to false ! # (!true or true) is same as (false or true) which evaluates to true and or
Prepend or Append Operators for Arrays
>> # Prpends to left side of array so 1 >> [2] results in [1,2] << # Appends to right side of array so 2 << [1] results in [1,2] + # [1] + 2 results in [1,2]. Array is always on the left-hand side of the operator
Flow Controls
do
* Creates a scope
%dw 2.0
output application/json
fun myfun() = do {
var msg = "Hello World!"
---
msg
}
fun sayhi(s: String) = do {
var msg = "Hello " ++ s ++ "!"
---
msg
}
---
{
result: myfun(),
result: sayhi("Jimmy")
}* Output
{
"result": "Hello World!",
"result": "Hello Jimmy!"
}if/else if/else
%dw 2.0 output application/json --- if (true) { result: "passed"} else { result: "failed"}
* Output
{
"result": "passed"
}References
* Introduction to Mule 4: DataWeave 2.0
* DataWeave Quickstart
* DataWeave Language