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.
Where is the metadata in the transform message module stored?
Whenever we create Metadata in the transform message module. The Metadata is stored in the application-types.xml file.
In XML Media Type we are supposed to specify the root tag?
True
How is the Transform Message structure?
Transform Message Structure consists of Header section and Body section.
What is the standard MIME Type in Mule?
application/dw
Two types of dataweave errors?
Scripting Error, Formatting error
What is Scripting error?
problem with syntax
What is Formatting error?
A problem with the transformation from one format to the other.
For example while transforming to application/xml from one format we may encounter formatting error as application/xml format expects a root tag.
How do we comment in dataweave?
for commenting in Dataweave we can either use //(double forward slash for single line comment) or /* */ (for multi line comment)
How do we specify attributes in XML?
use @(attName: attValue) to specify attributes in xml.
What is the syntax for specifying attributes while transforming from JSON to XML?
%dw 2.0 output application/xml --- { user @(fname: payload.firstName, lname: payload.lastName): { lname: payload.lastName } }
What is the syntax for fetching attributes from XML and transforming to JSON?
for following input/output
Input:
Muller
Output: { "fname": "Max" "lname": "Muller" }
{
fname: payload.user.@firstName,
lname: payload.user.lastName
}
How do we reference XML attributes in Datweave?
We can reference XML attributes in Dataweave using @ annotation.
What is Transformation Function (or Lambda)?
Inside the transformation function:-
- $$ refers to the index (or key)
- $ refers to the value
How to define the Global variable?
using var directive in the header we can define the Global Variable.
How does the variables behave in Dataweave?
variables in Dataweave behaves like function, because dataweave is a functional programming language.
What is the scope of Global variable in Dataweave?
Global variables in dataweave can be referenced anywhere in the body.
Defining Lambda expression in Dataweave?
We can define lambda expression in dataweave using following syntax:- %dw 2.0 output application/xml var lname = (aString) -> upper(aString) --- name: { first: payload.first, last: lname(payload.lastName) }
What is the syntax of using using?
using ( = )
What is the scope of local variable?
They can be accessed only within the scope where they are defined
How to enforce the arguments to be string?
var numSeats = (planeType: String) -> if (planeType contains(737)) 150 else 300
totalSeats: numSeats(Object.planeType as String)
How to create local variables?
we can create local variables using ‘using’ keyword.
What is usage of using keyword?
using ( flights = {“FirstName”: “Nikhil”, “LastName”: “Balyan”})
flights
Does dataweave supports Type coercion and formatting?
yes
What are defined types in dataweave?
Any, Null, String, Number, Boolean, Object, Array, Range, Date, Time, LocalTime, DateTime, LocalDateTime, TimeZone Period
How to format a number with 2 decimal points?
price as Number as String {format: “###.00” }
Can we define custom types in DW 2.0?
yes
How to define custom Types in DW 2.0?
using the type keyword
Syntax for importing dasherize functions in DW or any other function?
import dasherize from dw::core::Strings
What are lookup function?
It is used to call other flows and can only be used to call flows and not sub flows and is used inside dataweave.
What is the syntax of lookup function?
{a: lookup(“myFlow”, {“b”: “Hello”}) }
first parameter is the flow name and 2nd parameter is the payload to be send to the flow and the final response is returned to the calling flow.