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.
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…