DataWeave 2.0 Flashcards

1
Q

Which function do we use to iterate over a collection and produce a collection?

A

Map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which function do we use to iterate over a object and return a object?

A

MapObject

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which function do we use to iterate over an object and return a collection?

A

Pluck

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which function do we use to filter out a specific set of records?

A

Filter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which function works like a switch case/Pattern Matcher in Mule 4 Dataweave?

A

match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is when otherwise condition substitute in Mule 4?

A

if else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which DW function is used to concatenate 2 strings?

A

++

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which function is used to read the string or binary and return parsed content?

A

read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the input params required in read()?

A

(stringToParse, contentType, readerProperties) –> stringToParse and contentType are Mandatory params whereas readerProperties is optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write syntax of read()?

A

%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” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which function is used to read content from classpath or some REST API?

A

readURL()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the input parameters of readURL()?

A

(url, contentType, readerProperties) –> Here url and contentType are mandatory and readerProperties is optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is syntax of readURL() for reading from an endpoint and classpath?

A
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What applies a reduction expression to the elements in an array?

A

Reduce()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Reduce() does?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are reduce() input parameters?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Where is the sample dataweave used for preview?

A

src/test/resources

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to export dataweave code to DWL file?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How can we reference the dwl in transform message or any other module which uses dataweave?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Is it possible to create multiple transformations using single transform component? if yes then how?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Where is the metadata in the transform message module stored?

A

Whenever we create Metadata in the transform message module. The Metadata is stored in the application-types.xml file.

22
Q

In XML Media Type we are supposed to specify the root tag?

A

True

23
Q

How is the Transform Message structure?

A

Transform Message Structure consists of Header section and Body section.

24
Q

What is the standard MIME Type in Mule?

A

application/dw

25
Q

Two types of dataweave errors?

A

Scripting Error, Formatting error

26
Q

What is Scripting error?

A

problem with syntax

27
Q

What is Formatting error?

A

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.

28
Q

How do we comment in dataweave?

A

for commenting in Dataweave we can either use //(double forward slash for single line comment) or /* */ (for multi line comment)

29
Q

How do we specify attributes in XML?

A

use @(attName: attValue) to specify attributes in xml.

30
Q

What is the syntax for specifying attributes while transforming from JSON to XML?

A
%dw 2.0
output application/xml
---
{
 user @(fname: payload.firstName,
             lname: payload.lastName): {
             lname: payload.lastName
 }
}
31
Q

What is the syntax for fetching attributes from XML and transforming to JSON?
for following input/output
Input:

Muller 
Output:
{
 "fname": "Max"
 "lname": "Muller"
}
A

{
fname: payload.user.@firstName,
lname: payload.user.lastName
}

32
Q

How do we reference XML attributes in Datweave?

A

We can reference XML attributes in Dataweave using @ annotation.

33
Q

What is Transformation Function (or Lambda)?

A

Inside the transformation function:-

  • $$ refers to the index (or key)
  • $ refers to the value
34
Q

How to define the Global variable?

A

using var directive in the header we can define the Global Variable.

35
Q

How does the variables behave in Dataweave?

A

variables in Dataweave behaves like function, because dataweave is a functional programming language.

36
Q

What is the scope of Global variable in Dataweave?

A

Global variables in dataweave can be referenced anywhere in the body.

37
Q

Defining Lambda expression in Dataweave?

A
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)
}
38
Q

What is the syntax of using using?

A

using ( = )

39
Q

What is the scope of local variable?

A

They can be accessed only within the scope where they are defined

40
Q

How to enforce the arguments to be string?

A
var numSeats = (planeType: String) -> 
      if (planeType contains(737)) 
             150
        else 
             300

totalSeats: numSeats(Object.planeType as String)

41
Q

How to create local variables?

A

we can create local variables using ‘using’ keyword.

42
Q

What is usage of using keyword?

A

using ( flights = {“FirstName”: “Nikhil”, “LastName”: “Balyan”})
flights

43
Q

Does dataweave supports Type coercion and formatting?

A

yes

44
Q

What are defined types in dataweave?

A

Any, Null, String, Number, Boolean, Object, Array, Range, Date, Time, LocalTime, DateTime, LocalDateTime, TimeZone Period

45
Q

How to format a number with 2 decimal points?

A

price as Number as String {format: “###.00” }

46
Q

Can we define custom types in DW 2.0?

A

yes

47
Q

How to define custom Types in DW 2.0?

A

using the type keyword

48
Q

Syntax for importing dasherize functions in DW or any other function?

A

import dasherize from dw::core::Strings

49
Q

What are lookup function?

A

It is used to call other flows and can only be used to call flows and not sub flows and is used inside dataweave.

50
Q

What is the syntax of lookup function?

A

{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.