Let’s go over the anatomy of a DataWeave script using the code from the example:
DW Script:
%dw 2.0
input payload json
output csv header=false
—
payload
Let’s start anatomy of a DataWeave script using the below code.
%dw 2.0 input payload json output application/java --- payload
Note : If you’re using DW 1, you will always use %dw 1.0. If you’re using DW 2, you will always use %dw 2.0
input <var_name> <mime_type> [<reader_properties>] Note : The input directive is not required in some scenarios when the DataWeave execution itself is contextualized and the input derived from said context. For example, in Mule their event concept provides the context, adding inputs such as payload and vars.
output <mime_type> [<writer_properties>]
After the first three lines of the script there is a line only containing three dashes. This is to separate your declarations from your script output logic. You can also declare functions and variables that you can reuse in your script.
%dw 2.0 input payload json output yaml --- [ { "firstName": "John", "lastName": "Smith", "age": 45 }, { "firstName": "Jane", "lastName": "Doe", "age": 34 } ]
%YAML 1.2 --- - firstName: John lastName: Smith age: 45 - firstName: Jane lastName: Doe age: 34
Notice in the script that there are three lines, a line with three dashes, then one more line. The first three lines of the script contain directives. The first directive, which is in every DataWeave file, defines which version the script is using. You can think of this more as a necessary formality, as other factors will determine which DataWeave version is used to run your script (e.g., the Mule Runtime).
If you’re using DW 1, you will always use %dw 1.0. If you’re using DW 2, you will always use %dw 2.0.
The second and third lines contain the input and output directives. They each have their own form, which allows naming the inputs and defining reader and writer properties:
input <var_name> <mime_type> [<reader_properties>]
output <mime_type> [<writer_properties>]
After the first three lines of the script there is a line only containing three dashes. This is to separate your declarations from your script output logic, sometimes called the header and the body of the script respectively. You’ll see in later tutorials that you can do more than just specify input and output directives in the declarations section, you can also declare functions and variables that you can reuse in your script.
Finally, the last line of the script is the output section. Whatever the output section evaluates to is what gets sent to the writer, and is ultimately serialized into the specified output format.
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…