DataWeave 2.0 Flashcards
Which function do we use to iterate over a collection and produce a collection?
Map
Which function do we use to iterate over a object and return a object?
MapObject
Which function do we use to iterate over an object and return a collection?
Pluck
Which function do we use to filter out a specific set of records?
Filter
Which function works like a switch case/Pattern Matcher in Mule 4 Dataweave?
match
What is when otherwise condition substitute in Mule 4?
if else
Which DW function is used to concatenate 2 strings?
++
Which function is used to read the string or binary and return parsed content?
read
What are the input params required in read()?
(stringToParse, contentType, readerProperties) –> stringToParse and contentType are Mandatory params whereas readerProperties is optional.
Write syntax of read()?
%dw 2.0
output application/xml
—
read(‘{ “hello” : “world” }’,’application/json’)
output:–
world
2- %dw 2.0 var myVar = "Some, Body" output application/json --- read(myVar,"application/csv",{header:false})[0]
output:—
{ “column_0”: “Some”, “column_1”: “ Body” }
Which function is used to read content from classpath or some REST API?
readURL()
What are the input parameters of readURL()?
(url, contentType, readerProperties) –> Here url and contentType are mandatory and readerProperties is optional
What is syntax of readURL() for reading from an endpoint and classpath?
1- Reading data from endpoint:- source:- %dw 2.0 output application/json --- readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
output:-
{ “userId”: 1, “id”: 1, “title”: “sunt aut …”, “body”: “quia et …” }
2- Reading data from classpath:-
source:-
%dw 2.0
var myJsonSnippet = readUrl(“classpath://myJsonSnippet.json”, “application/json”)
output application/csv
—
(myJsonSnippet.results map(item) -> item.profile)
What applies a reduction expression to the elements in an array?
Reduce()
What does Reduce() does?
for each element of the input array, in order, reduce applies the reduction of lambda expression(function), then replaces the accumulator with the new result. The lambda expression can use both the current input array element and the current accumulator value.
What are reduce() input parameters?
1- item
Item in the input array. It provides the value to reduce. can also be referenced using $.
2-acc
The accumulator, Can also be referrenced using $$, Used to store the result of lambda expression after each iteration of the reduce operation.
The accumulator parameter can be set to an initial value using the syntax acc = initValue. In this case, the lambda expression is called with the first element of the input array. Then the result is set as the new accumulator value.
If an initial value for the accumulator is not set, the accumulator is set to the first element of the input array. Then the lambda expression is called with the second element of the input array.
The initial value of the accumulator and the lambda expression dictate the type of result produced by the reduce function. If the accumulator is set to acc = {}, the result is usually of type Object. If the accumulator is set to acc = [], the result is usually of type Array. If the accumulator is set to acc = “”, the result is usually a String.
Where is the sample dataweave used for preview?
src/test/resources
How to export dataweave code to DWL file?
Step 1:- Click on edit icon in the transform message
Step 2:- Specify the path of file where we want to store dataweave like dwl/dummy.dwl
Step 3:- By now we have externalized the dataweave code to dwl file.
How can we reference the dwl in transform message or any other module which uses dataweave?
- In transform message we can reference using resource attribute.
- In an element that has an expression property(like the choice router) using the ${file::filename} syntax.
- We can also create modules(libraries) of reusable Dataweave functions.
Is it possible to create multiple transformations using single transform component? if yes then how?
- It is possible to create multiple transformations using single transform component. By leveraging the Mule transform module and adding multiple variables in a single transform message Module.