Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

DataWeave Cookbook

This section links to examples that demonstrate common data extraction and transformation approaches.

Example Description

Extracting Data

Shows common selector expressions for extracting values from a data source, such as a Mule message.

Perform Basic Transformation

Uses selectors to perform a basic format transformation. It does not use any functions.

Mapping Data

Uses map to reorganize fields in a JSON array. It uses as to coerce a String into a Number.

Map an Object

Uses mapObject to go through the keys and values in each object, and set all the keys to upper case.

To Map Object Elements as 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.

Renaming Keys

Renames some keys in a JSON object while retaining the names of all other keys in the output. Uses mapObject, if, as, and and.

Outputting 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.

Regroup Fields

Uses groupBy, mapObject, and map to reorganize JSON and XML fields.

Zip Arrays Together

Uses zip to rearrange pairs of similar arrays so that they form a series of touples with the matching values from each.

Pick Top Elements

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.

Changing the Value of a Field

Masks sensitive data by changing values of some keys to asterisks (****`). Uses mapObject, if, and else.

Excluding 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.

Defining a Function that Flattens Data in a List

Uses reduce, map, if, and splitBy to modify and conditionally output fields from a list.

Outputting self-closing XML tags

Uses inlineCloseOn="empty" to close empty tags (for example, <element2/>).

Inserting an Attribute into an XML Tag

Uses the @(key:value) syntax to create an XML attribute.

Remove Certain 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).

Include XML Namespaces

Defines multiple XML namespaces and references these in each tag.

Reference Multiple Inputs

References data on the payload, a message attribute and a variable. It then processes these through map, filter and multiplications.

Merge Multiple Payloads

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.

Using Constant Directives

Defines a var in the DataWeave header that builds URLs from constant directives using ++ to concatenate strings.

Parsing Dates

Defines a function that normalizes conflicting date formats into a single, common format. Uses mapObject, replace and as to coerce a data type.

Adding and Subtracting Dates

Performs multiple math operations combining different types related to date and time.

Conditionally Reduce a List Via 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.

Dynamic Mapping 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.

Passing 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.

In this topic: