SCRIPT ANATOMY

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

Script Anatomy

Let’s start anatomy of a DataWeave script using the below code.

DW Script
%dw 2.0
input payload json
output application/java
---
payload
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.
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
The second lines contain the input directives. Which allows naming the inputs and defining reader properties.
input <var_name&gt; <mime_type&gt; [<reader_properties&gt;]

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.
The third lines contain the output directives. Which allows naming the outputs and defining writer properties.
output <mime_type&gt; [<writer_properties&gt;]

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.

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.
DW Script
%dw 2.0
input payload json
output yaml
---
[
  {
    "firstName": "John",
    "lastName": "Smith",
    "age": 45
  },
  {
    "firstName": "Jane",
    "lastName": "Doe",
    "age": 34
  }
]
Output
%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.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago