Dataweave Flashcards
What is dataweave ?
Dataweave is a mulesoft expression language for accessing and transforming data that travels through a mule application.
Define and call a variable ?
%dw 2.0 var myJson = {"Hello" : "World"} var cr=10 output application/json --- { myObjectVar : myJson, price: cr*3 }
Output: { myObjectVar: {"Hello" : "World"}, price: 30 }
Use a dataweave function in a dataweave variable ?
%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, Transform and select content from an input
%dw 2.0 var myRead = read("red", "application/xml") output application/json --- { mySelection : myRead.car }
Output:
{ "mySelection": { "color": "red" } }
What are the sections/parts in Dataweave script?
Header and Body
Concatenate Two Strings into a Single String
%dw 2.0
output application/json
—
{ myString: (“hello” ++ “World”) }
Output:
{
“myString”: “helloWorld”
}
What does the header contains?
Dataweave declaration : %dw 2.0 .. . . . . .
What is this declaration called ?
output application/json
Output directive
Read File Contents with a DataWeave Function
1. Create a file src/main/resources/myjson.json { "hello": "world" } 2. Dataweave script %dw 2.0 output application/json --- readUrl("classpath://myJson.json", "application/json")
- Output:
{
“hello”: “world”
}
Extract only values from key value pairs in json
Use the pluck function %dw 2.0 output application/json --- { "0": "a", "1": "b", "2": "c" } pluck ((value) -> value)
Output: [ "a", "b", "c" ]
What does $$ indicate in map operator
Index of the array
What does $ indicate in map operator
The value/data at an index in the array
Filter an array with condition value >2
Input:
[9,2,3,4,5]
[9,2,3,4,5] filter (value,index) -> (value > 2)
[9,2,3,4,5] filter ($ >2)
Output:
[9,3,4,5]
Filter an array with condition index > 2
Input:
[9,2,3,4,5]
[9,2,3,4,5] filter (value,index -> (index > 2)
[9,2,3,4,5] filter ($$ >2)
Output:
[4,5]
Filter an array with condition index > 1 and value <5
Input:
[9,2,3,4,5]
[9,2,3,4,5] filter (($$ > 1) and ($ < 5))
Output:
[3,4]
What are these called ?
$$ and $
Anonymous parameters.
These are used in place of named parameters in anonymous function
How do you define date and time ?
Enclosed in pipes.
myDate: |2018-12-07|,
myTime: |11:55:56|,
myDateTime: |2018-10-01T23:57:59-03:00|,
Object with condition field ?
myObjectWithConditionalField: { a : { b : 1, ( c : 2 ) if true, (d : 4) if false } },
Define string as binary
myBinary: “abcd1234123” as Binary
What is pluck function and how to use it ?
Pluck function iterates over a json object and can be used to output values into an array. %dw 2.0 output application/json --- { "0": "a", "1": "b", "2": "c" } pluck ((value) -> value)
Output: [ "a", "b", "c" ]
Conditional check on a payload
ContainsRequestedItem: myInput.root.order.items contains “3”
Get current time ?
currTime: now()
Define and call a function ?
fun toUser(obj) = {
firstName: obj.field1,
lastName: obj.field2
}
What is the default output directive ?
application/java
What can dataweave header contain ?
Declaration ( %dw 2.0) Import DW function module (import * from dw::core::Arrays) functions ( fun myFunc(obj) = {fname=obj.field1} ) output directive ( output application/xml ) variables (var myVar = 12.32) Name spaces (ns ns0 http://abc.com) Define custom types (type)
What are custom types and how to use them in dw script ?
?
What are namespaces and how to define and use them in dw script ?
?
What is new in DW 2.0 ?
Operators are functions:
DW1 : sizeOf payload filter $.age > 30
DW2: sizeOf(payload filter $.age > 30)
Traits are functions:
DW1 : payload is :empty
DW2: isEmpty(payload)
Type Names:
DW1: payload.foo as :string
DW2: payload.foo as String
Ability to call out to java code
Modules and imports
What are standalone and inline DW scripts
Standalone scripts are the one’s that are written in transform component.
Inline DW expressions are to used to transform data in place and dynamically set values of various properties of components such as choice router, set payload or set variable component
What is importing modules and how to do it?
DW 2.0 functions are packaged in modules.
Functions in the core (dw::core) are imported automatically.
import dw::core::Strings
import camelize, capitalize from dw::core::Strings
import * from dw::core::Strings
Examples: %dw 2.0 import dw::core::Strings output application/json { "plural" : Strings::plurarize("box")}
%dw 2.0
import pluralize from dw::core::Strings
output application/json
{ “plural” “ pluralize(“box”) }
Give some examples on string manipulation functions?
camelize:
{“a”: camelize(“Customer_first_name”)” O/P “a”: CustomerFirstName
capitalize:
“a” : capitalize(“customer”) O/P “a” : “ Customer”
“b” : capitalize(“customer_first_name”) O/P “b” : “Customer First Name
isLowerCase:
“a” : isLowerCase(“abc”) O/P “a” : true
isUpperCase:
“a” : isUpperCase(“abc”) O/P “a”: false
pluralize:
“a” : pluralize(“box”) O/P “a”: boxes
singularize:
“a”: singularize(“boxes”) O/P “a” : box
What are selectors and how to use them?
Selectors allow you to extract data from objects, arrays, variables and payload.
var myObject : { “myKey” : “1234”, “name”: “somebody” }
var myArray: [ {“myKey” : “12345”}, { “name” : “somebody” } ]
—
{
fromObject: myObject.name,
fromArray: myArray.name
}
Output: { "fromObject" : somebody, "fromArray" : [somebody] }
Different selectors?
Single value selector (.myKey) Multivalue selector (.*) keyvalue pair (.&myKey) Decendents(..myKey) index([]) XML Attribute (.@mykey) Namespace selector (mykey.#) Range [index1 to index2] Selector Modifier (? and !) Filter selectors ( mykey[?(booleanExpression)]) Metadata Selector (.^someMetadata)