DataWeave Examples | MuleSoft Documentation
# DataWeave Examples
The following DataWeave examples demonstrate common data extraction and transformation approaches. There are DataWeave code examples of how to transform data, and also examples of Mule applications that implement DataWeave transformations.
## DataWeave Code Examples
These examples show how to use standalone DataWeave to extract and transform data.
Example | Description  
---|---  
|  Shows common selector expressions for extracting values from a data source, such as a Mule message.  
|  Uses a single-value DataWeave selector (`.`) to extract data from XML elements.  
|  Shows some common ways to set default values.  
Set Reader and Writer Configuration Properties |  Shows how to use and change the settings for reading and writing data formats, such as `application/csv`.  
|  Uses selectors to perform a basic format transformation. It does not use any functions.  
Uses `map` to reorganize fields in a JSON array. It uses `as` to coerce a String into a Number.  
Map and Flatten an Array |  Uses `flatMap` to map objects in an array and flatten the resulting array.  
Uses `mapObject` to go through the keys and values in each object, and set all the keys to upper case.  
|  Uses the `mapObject` function to iterate through an array of objects and appends a new object that matches the value of the specified criteria.  
Map the Objects within an Array |  Uses the multi-value selector to pick out the keys in an object that are named "book", these are returned as an array. It then uses `map` to iterate through the array this creates.  
Dynamic Map Based on a Definition |  Transforms the payload according to definitions sent in a variable. It defines a function that applies the logic defined in the variable, using `map` and `default`.  
Renames some keys in a JSON object while retaining the names of all other keys in the output. Uses `mapObject`, `if`, `as`, and `and`.  
Output a Field When Present |  Uses `if` with `map` to determine whether to include a field in the output. You might use it to exclude fields that contain sensitive data.  
Format According to Type |  Uses `mapObject` to apply changes to the keys in an object, depending on the type of their corresponding values. Uses `camelize`, `capitalize`, and `pluralize` depending on the `if` and `else if` statement.  
|  Uses `groupBy`, `mapObject`, and `map` to reorganize JSON and XML fields.  
|  Uses `zip` to rearrange pairs of similar arrays so that they form a series of tuples with the matching values from each.  
|  Uses `groupBy` and `map` to sort a list of candidates according to their score at a test, then it splits the array to select only the top candidates.  
Change the Value of a Field |  Masks sensitive data by changing values of some keys to asterisks (`****``). Uses `mapObject`, `if`, and `else`.  
Exclude Fields from the Output |  Shows how to exclude unwanted elements from the output. Uses the `-` (remove) and `mapObject` functions.  
Use Constant Directives |  Defines a series of constant strings and numbers in the header, these are used to filter input and to concatenate into URLs. Uses `map`, `if` and `++` to concatenate strings.  
Define a Custom Addition Function |  Defines a function that obtains totals and subtotals through the `accumulator` function. Also performs additions, subtractions, and multiplications of numeric values.  
Define a Function That Flattens Data in a List |  Uses `reduce`, `map`, `if`, and `splitBy` to modify and conditionally output fields from a list.  
Flatten Elements of Arrays |  Uses `flatten`, the object constructor braces (`{}`), and selectors to flatten subarrays and nested elements in arrays. Uses concatenation (`++`) to combine arrays before flattening them and uses the `..*` descendants selector on a key to select values from a flattened list.  
Use Regular Expressions |  Shows uses of regular expressions in arguments to several DataWeave functions.  
Output Self-closing XML Tags |  Uses `inlineCloseOn="empty"` to close empty tags (for example, `<element2/>`).  
Insert an Attribute into an XML Tag |  Uses the `@(key:value)` syntax to create an XML attribute.  
Remove Specified XML Attributes |  Defines a function that recursively checks an element and all of its children for a specific XML attribute and removes it with `-`. It also uses `mapObject`, `if` and `is` (to match types).  
|  Pass XML attributes from the input source payload to the output XML. It uses the dynamic attribute expression `@dynamicAttributes` to create the attributes of the new output tag by selecting the attributes dynamically from the input.  
Include XML Namespaces |  Defines multiple XML namespaces and references these in each tag.  
Remove Objects Containing Specified Key-Value Pairs |  Removes all objects that contain a set of key-value pairs from an array of objects. Uses `filter` with `contains` and a `not` operator.  
Reference Multiple Inputs |  References data on the payload, a message attribute and a variable. It then processes these through `map`, `filter` and multiplications.  
Merge Fields from Separate Objects |  Reference data that arrives in multiple separate payloads from one single Mule event. Filters the output based on a unique identifier. Uses `map`, `using`, `as`, and `filter` functions.  
Defines a function that normalizes conflicting date formats into a single, common format. Uses `mapObject`, `replace` and `as` to coerce a data type.  
Add and Subtract Dates |  Performs multiple math operations combining different types related to date and time.  
|  Uses with a time zone ID to change a time zone and provides a list of available time zone IDs.  
Format Dates and Times |  Uses formatting characters such as `uuuu/MM/dd` to change the format of dates and times. Creates a date-time format using a custom DataWeave type.  
Work with Multipart Data |  Uses `mapObject` to iterate over a multipart payload and extract data from each part. It also uses `read` with a `boundary` value to read the multipart content.  
Conditionally Reduce a List through a Function |  Defines a function that reduces a list of elements into one field. It then calls this function only when a certain field has values. Uses `map`, `reduce`, `splitBy`, `if` and `++` to append to an array.  
Pass Functions as Arguments |  Defines a function that expects to receive two inputs: a function to apply and an element to apply it on. The function is also recursively applied to the element’s children. It uses `mapObject` `lower` `is` `if/else`.  
## DataWeave Code in MuleSoft Applications Examples
These examples show how to use DataWeave to extract and transform data in MuleSoft applications.
Example | Description  
---|---  
Change a Script’s Output Mime Type |  Shows how to customize the MIME type of a given format output, including an example of a JSON output with `application/problem+json` MIME type.  
Look Up Data in an Excel (XLSX) File |  Uses `filter` to return rows that contain a specified value.  
Look Up Data in CSV File |  Uses `filter` with `map` to look up data in a CSV file and return a country code for each calling code found within an input array of phone numbers.  
Decode and Encode Base64 |  Converts a file stream into Base64 and converts a Base64 string into a file stream. Uses a PDF file as the input and output.  
Call Java Methods with DataWeave |  Calls Java methods from a Java class in a Mule project.  
Read and Write a Flat File |  Use DataWeave to read and write flat files in a Mule application.  
Use a Reader Property through a Connector |  Use a reader property through a Connector operation.  
Use Dynamic Writer Properties |  Use a Mule variable as a configuration value.  
Extract Key/Value Pairs with Pluck Function |  Use the DataWeave `pluck` function to extract key-value pairs from a JSON payload and store them into Mule event variables.  
## See Also
  * DataWeave Language
  *   * DataWeave Reference