11_DataWeave Flashcards
By default, where is the dataweave code?
inline in the Mule configuration file
Where is sample data used for live preview stored?
src/test/resources
Where are the dwl files stored?
src/main/resources
dwl stand for?
data weave language
Where in the Transform Message component can you reference to use an external DWL file?
resource attribute
Where in an element that has an expression property (e.g. choice router), can you use reference to an external DWL file?
using syntax ${file::filename}
Can you create modules (libraries) of reusable Dataweave functions?
yes
How do you create multiple transformations with one Transform Message Component?
- Add new target button (lines and a plus sign), 2. Set where to store result (Variables or Attributes or…) 3. Switch between multiple transformation (click on the dropdown.
The data model of the produced output of the dataweave expression could consist of what 3 data types?
Object. Array. Simple literals.
What is an object?
Key : value pairs.
What is an Array?
A sequence of comma separated values.
How is a dataweave expression separated into header and body?
Delimiter to separate header and body
What does the header of a dwl file contain?
directives that apply to the body expression
What does the body of a dwl file contain?
DataWeave expression that generates the output structure
Give some example mime type (formats) that the script will output
application/json, application/java, application/xml, text/plain, application/dw, application/csv, application/xlsx, …
What are the 2 types of DataWeave errors?
Scripting errors (syntax problems) and Formatting errors (a problem with how the transformation takes one format to another, there could be an issue if you are outputting an XML but the structure does no have a single root node))
If you get an error what can you do to check what type of error it is?
transform the input to application/dw, then if the transformation is successful it’s likely to be a formatting error
Dataweave syntax for a applcation/json output, fname: payload.firstname and {fname: payload.firstname} produce what outputs (if the firstname in the payload is “RayJones”?
{fname: “RayJones”} and {fname: “RayJones”}
same
What does an XML file need?
A root level (and only one).
Will this dwl code with an application/xml output write? — fname: payload.first, lname: payload.last
No, as it doesn’t have a one top-level value
How do you specify an attribute for an XML output? (you can have multiple attributes within the brackets).
@(attName: attValue)
Are attributes written to XML output as default?
No
XML input has an attribute ? Output application/json. What is the dataweave syntax to access the attribute?
payload.name.@initials
(which would give you
“ww”)
When using the map function what can the input array be?
JSON or Java
What does a map function do to an array?
IT takes each element in the array and applies the transformation function (or lambda function)
What does the map function return?
An array. (just think, if you are inputting an array and taking every value and transforming each value, it puts it back in the array).
Where is the transformation function used?
In the map function.
Within the transformation function what does the $ refer to?
value
Within the transformation function what does the $$ refer to?
Index
In dwl where can you find the syntax $.firstname
Within the transformation function (therefore, in the map function)
What does the $$ in the transformation function start from?
0 (index starts at 0, think it’s handling arrays - so makes sense)
What is payload map (object, index) -> { num: index, fname:object.fname} the same syntax as?
payload map (num: $$, fname: $.firstname)
What does wrapping Java/JSON array elements with {()} do?
If the output is application/xml, then it will convert the array to an XML object
How do you change a Java/JSON array to application/xml?
You have to wrap the map operation in the {()}. If you do not do so, the array will error ‘Cannot coerce an array object’).
You can wrap the elements either around the map function or around the map operation, true or false?
true
What do you use the .* selector for?
To reference repeated elements.
How do you reference repeated elements?
Use the .* selector.
If you are converting XML to JSON and dwl has the payload.lname whilst there are repeated tags of the lname, what is the JSON output?
lname (and only the first value. If you want to have all the repeated references you need to include the .*
XML -> JSON: What is the output if the input is (
Mule
Jennet
). DWL code: payload.users.*user map (obj,index) -> {
fname: obj.@firstname,
lname: obj.lastname
}
[
{“fname”: “Max”, “lname”: “Mule” },
{“fname”: “Molly”, “lname”: “Jennet” }
]
Where can global variables can be referenced?
anywhere in the body
Where should you use the var directive in the dwl code?
In the header.
What does the dwl code (var name) = “the” do?
states the global variable name to the, which can be used throughout the body of the code (acts as functional programming language).
What does the code (var name = () -> “wow”) ?
When you call the variable name, with the name() it will replace it with “wow”.
What does the dwl code produce, if the input is {"lname": "plants"} and the dwl has (output application/xml var mname = "the" var mname2 = () -> "other" var lname = (aString) -> upper(aString) --- name: { first: payload.firstname, middle1: mname, middle2: mname2(), last: lname(payload.lastname) })?
plants
the
other
PLANTS
What are the 2 different types of syntax for accessing lambda expressions within dataweave?
I think it’s referring to the var and fun. ??? check
What is the 2 alternative syntaxs to access lambda expressions?
var functionName = (aString) -> function(aString) OR fun functionName(aString) = function(aString)
Where can you use the keyword using?
in the body of the dwl code
Where can local variables be referenced?
In the scope of the expression where they are initialized
json -> json
What does this dwl code output? using (fname = payload.firstname, lname = payload.lastname) { person: using (user = fname, color = “gray”) { name1: user, color: color }, name2: lname, color: color }
Unable to resolve reference of color.
Explain using ( = )
It is a local variable that can only be referenced within the scope that it is initialized. e.g. using (fname = “I’m” ++ payload.firstname) { You can use it here } You can’t use it here.
How do you format data?
{format: “”} (and in between the quotation marks you add in the format you’d like to output)
If you want to change the data type, what operation do you use?
as (e.g. as Number, as String)
How do you define a custom data type?
using the type header directive (name has to be all lower case, no special chars, or numbers)
What is this dwl code doing? (type = ourdateformat = DateTime {format: “yyyyMMddHHmm”})
This is setting a custom data type named ‘ourdateformat’, which is a DateTime with the format ‘yyyyMMddHHmm’.
You’ve created a custom data type called sweetpotato, you want to use it in the body of the dwl code, how do you use it?
payload.normalPotato as sweetpotato
What does POJO stand for?
Plain old java object
2 ways to transform an oject to a POJO?
- specify inline
2. define a custom data type and use that instead
Define a custom data type to transform an object to a POJO
type custompojo = Object {class: “”}
customer: payload.blah as custompojo
(the type is defining the custom data type, the custompojo is the name of the custom data type, Object is datatype with the class stating what java class it should be) —-(the last statement is changing the field blah from the payload to a pojo)
Define how to transorm an object to a POJO by specifying inline.
customer: payload.blah as Object {class: “”}
think POJO - java - class, as it will need a class… think think think
What are dw functions packaged into?
modules
What functions are automatically imported into dataweave scipts?
core modules
Functions that have 2 params, have 2 alternative syntax, what are they?
[contains (payload, max)] OR #[payload contains “max”]
the function here being the contains
e.g. #[function(param1, param2)] OR #[param1 function “param2”]
When using a series of functions, how are they executed?
From the right to the left (the last function in the chain is executed first)
How do you get something that is an expression using the map function to be evaluated first if it’s at the beginning of a series of functions
You can use the ‘using’ to define it as a local variable.
How do you import a function in a module?
import functionName from dw::core::module
How do you import a module
import dw::core:module
basically import module name
What does the dasherize(String) function do?
replaces spaces, underscores and camel-casing in a string with dashes (hyphens).
How do you call a flow in dwl?
Use the lookup function
lookup(“flowName”, payload)
Can you call sub flows in dwl using the lookup?
No.
What does the lookup function return?
Whatever the payload the flow returns is what the expression returns
What is a lambda?
An anonymous function not bound to an identifier.
What is at the top of the precendence order before binary functions?
using() function
What header is used to import functions?
import
What does the as Object {class: “”} syntax denote?
transforming objects to POJOs