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]