Dataweave Flashcards

1
Q

What is dataweave ?

A

Dataweave is a mulesoft expression language for accessing and transforming data that travels through a mule application.

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

Define and call a variable ?

A
%dw 2.0
     var myJson = {"Hello" : "World"}
     var cr=10
     output application/json
     ---
    {
         myObjectVar : myJson,
         price: cr*3
    }
     Output:
     {
          myObjectVar: {"Hello" : "World"},
         price: 30
     }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use a dataweave function in a dataweave variable ?

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

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

Read, Transform and select content from an input

A
%dw 2.0
var myRead = read("red",                "application/xml")
output application/json
---
{  
mySelection : myRead.car
}

Output:

{
  "mySelection": {
    "color": "red"
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the sections/parts in Dataweave script?

A

Header and Body

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

Concatenate Two Strings into a Single String

A

%dw 2.0
output application/json

{ myString: (“hello” ++ “World”) }

Output:
{
“myString”: “helloWorld”
}

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

What does the header contains?

A
Dataweave declaration : %dw 2.0
..
.
.
.
.
.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is this declaration called ?

output application/json

A

Output directive

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

Read File Contents with a DataWeave Function

A
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")
  1. Output:
    {
    “hello”: “world”
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Extract only values from key value pairs in json

A
Use the pluck function 
%dw 2.0
output application/json
---
{
  "0": "a",
  "1": "b",
  "2": "c"
} pluck ((value) -> value)
Output:
[
  "a",
  "b",
  "c"
]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does $$ indicate in map operator

A

Index of the array

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

What does $ indicate in map operator

A

The value/data at an index in the array

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

Filter an array with condition value >2
Input:
[9,2,3,4,5]

A

[9,2,3,4,5] filter (value,index) -> (value > 2)
[9,2,3,4,5] filter ($ >2)
Output:
[9,3,4,5]

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

Filter an array with condition index > 2
Input:
[9,2,3,4,5]

A

[9,2,3,4,5] filter (value,index -> (index > 2)
[9,2,3,4,5] filter ($$ >2)
Output:
[4,5]

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

Filter an array with condition index > 1 and value <5
Input:
[9,2,3,4,5]

A

[9,2,3,4,5] filter (($$ > 1) and ($ < 5))
Output:
[3,4]

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

What are these called ?

$$ and $

A

Anonymous parameters.

These are used in place of named parameters in anonymous function

17
Q

How do you define date and time ?

A

Enclosed in pipes.
myDate: |2018-12-07|,
myTime: |11:55:56|,
myDateTime: |2018-10-01T23:57:59-03:00|,

18
Q

Object with condition field ?

A

myObjectWithConditionalField: { a : { b : 1, ( c : 2 ) if true, (d : 4) if false } },

19
Q

Define string as binary

A

myBinary: “abcd1234123” as Binary

20
Q

What is pluck function and how to use it ?

A
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"
]
21
Q

Conditional check on a payload

A

ContainsRequestedItem: myInput.root.order.items contains “3”

22
Q

Get current time ?

A

currTime: now()

23
Q

Define and call a function ?

A

fun toUser(obj) = {
firstName: obj.field1,
lastName: obj.field2
}

24
Q

What is the default output directive ?

A

application/java

25
Q

What can dataweave header contain ?

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

What are custom types and how to use them in dw script ?

A

?

27
Q

What are namespaces and how to define and use them in dw script ?

A

?

28
Q

What is new in DW 2.0 ?

A

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

29
Q

What are standalone and inline DW scripts

A

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

30
Q

What is importing modules and how to do it?

A

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

31
Q

Give some examples on string manipulation functions?

A

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

32
Q

What are selectors and how to use them?

A

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]
}
33
Q

Different selectors?

A
Single value selector (.myKey)
Multivalue selector (.*)
keyvalue pair (.&amp;myKey)
Decendents(..myKey)
index([])
XML Attribute (.@mykey)
Namespace selector (mykey.#)
Range [index1 to index2]
Selector Modifier (? and !)
Filter selectors ( mykey[?(booleanExpression)])
Metadata Selector (.^someMetadata)