---
schema_version: "1"
id: fdaf4aaf9fb86ea9657ee8a5c1427deb77e0cd951d9fcf87b7f7a36bb62a160f
title: DataWeave Examples
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dataweave-cookbook.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: ebcfa63b2958967a794db7ed39e31cfd6f34e5f6288006ab31ae8df38a082e54
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping, examples
---

# DataWeave Examples

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The examples demonstrate common data extraction and transformation approaches.

<table><colgroup><col> <col></colgroup><thead><tr><th>Example</th><th>Description</th></tr></thead><tbody><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-extract-data">Extract Data</a></p></td><td><div><div><p>Shows common selector expressions for extracting values from a data source, such as a Mule message.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-select-xml-elements">Select XML Elements</a></p></td><td><div><div><p>Uses a single-value DataWeave selector (<code>.</code>) to extract data from XML elements.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-defaults">Set a Default Value</a></p></td><td><div><div><p>Shows some common ways to set default values.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-set-reader-writer-props">Set Reader and Writer Configuration Properties</a></p></td><td><div><div><p>Shows how to use and change the settings for reading and writing data formats, such as <code>application/csv</code>.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-perform-basic-transformation">Transform XML to JSON</a></p></td><td><div><div><p>Uses selectors to perform a basic format transformation. It does not use any functions.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-change-script-output-mime">Change a Script’s Output Mime Type</a></p></td><td><div><div><p>Shows how to customize the MIME type of a given format output, including an example of a JSON output with <code>application/problem+json</code> MIME type.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map">Map Data</a></p></td><td><div><div><p>Uses <code>map</code> to reorganize fields in a JSON array. It uses <code>as</code> to coerce a String into a Number.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map-and-flatten">Map and Flatten an Array</a></p></td><td><div><div><p>Uses <code>flatMap</code> to map objects in an array and flatten the resulting array.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map-an-object">Map Objects</a></p></td><td><div><div><p>Uses <code>mapObject</code> to go through the keys and values in each object, and set all the keys to upper case.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map-an-object-key">Map Objects Key</a></p></td><td><div><div><p>Uses the <code>mapObject</code> function to iterate through an array of objects and appends a new object that matches the value of the specified criteria.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map-object-elements-as-an-array">Map the Objects within an Array</a></p></td><td><div><div><p>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 <code>map</code> to iterate through the array this creates.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map-based-on-an-external-definition">Dynamic Map Based on a Definition</a></p></td><td><div><div><p>Transforms the payload according to definitions sent in a variable. It defines a function that applies the logic defined in the variable, using <code>map</code> and <code>default</code>.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-xlsx-lookup">Look Up Data in an Excel (XLSX) File</a></p></td><td><div><div><p>Uses <code>filter</code> to return rows that contain a specified value.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-csv-lookup">Look Up Data in CSV File</a></p></td><td><div><div><p>Uses <code>filter</code> with <code>map</code> 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.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-decode-encode">Decode and Encode Base64</a></p></td><td><div><div><p>Converts a file stream into Base64 and converts a Base64 string into a file stream. Uses a PDF file as the input and output.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-java-methods">Call Java Methods with DataWeave</a></p></td><td><div><div><p>Calls Java methods from a Java class in a Mule project.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-rename-keys">Rename Keys</a></p></td><td><div><div><p>Renames some keys in a JSON object while retaining the names of all other keys in the output. Uses <code>mapObject</code>, <code>if</code>, <code>as</code>, and <code>and</code>.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-output-a-field-when-present">Output a Field When Present</a></p></td><td><div><div><p>Uses <code>if</code> with <code>map</code> to determine whether to include a field in the output. You might use it to exclude fields that contain sensitive data.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-format-according-to-type">Format According to Type</a></p></td><td><div><div><p>Uses <code>mapObject</code> to apply changes to the keys in an object, depending on the type of their corresponding values. Uses <code>camelize</code>, <code>capitalize</code>, and <code>pluralize</code> depending on the <code>if</code> and <code>else if</code> statement.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-regroup-fields">Regroup Fields</a></p></td><td><div><div><p>Uses <code>groupBy</code>, <code>mapObject</code>, and <code>map</code> to reorganize JSON and XML fields.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-zip-arrays-together">Zip Arrays Together</a></p></td><td><div><div><p>Uses <code>zip</code> to rearrange pairs of similar arrays so that they form a series of touples with the matching values from each.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-pick-top-elements">Pick Top Elements</a></p></td><td><div><div><p>Uses <code>groupBy</code> and <code>map</code> to sort a list of candidates according to their score at a test, then it splits the array to select only the top candidates.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-change-value-of-a-field">Change the Value of a Field</a></p></td><td><div><div><p>Masks sensitive data by changing values of some keys to asterisks (<code>****`</code>). Uses <code>mapObject</code>, <code>if</code>, and <code>else</code>.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-exclude-field">Exclude Fields from the Output</a></p></td><td><div><div><p>Shows how to exclude unwanted elements from the output. Uses the <code>-</code> (remove) and <code>mapObject</code> functions.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-use-constant-directives">Use Constant Directives</a></p></td><td><div><div><p>Defines a series of constant strings and numbers in the header, these are used to filter input and to concatenate into URLs. Uses <code>map</code>, <code>if</code> and <code>++</code> to concatenate strings.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-define-a-custom-addition-function">Define a Custom Addition Function</a></p></td><td><div><div><p>Defines a function that obtains totals and subtotals through the <code>accumulator</code> function. Also performs additions, subtractions, and multiplications of numeric values.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-define-function-to-flatten-list">Define a Function That Flattens Data in a List</a></p></td><td><div><div><p>Uses <code>reduce</code>, <code>map</code>, <code>if</code>, and <code>splitBy</code> to modify and conditionally output fields from a list.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-flatten-arrays">Flatten Elements of Arrays</a></p></td><td><div><div><p>Uses <code>flatten</code>, the object constructor braces (<code>{}</code>), and selectors to flatten subarrays and nested elements in arrays. Uses concatenation (<code>++</code>) to combine arrays before flattening them and uses the <code>..*</code> descendants selector on a key to select values from a flattened list.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-use-regex">Use Regular Expressions</a></p></td><td><div><div><p>Shows uses of regular expressions in arguments to several DataWeave functions.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-output-self-closing-xml-tags">Output Self-closing XML Tags</a></p></td><td><div><div><p>Uses <code>inlineCloseOn="empty"</code> to close empty tags (for example, <code>&lt;element2/&gt;</code>).</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-insert-attribute">Insert an Attribute into an XML Tag</a></p></td><td><div><div><p>Uses the <code>@(key:value)</code> syntax to create an XML attribute.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-remove-certain-xml-attributes">Remove Specified XML Attributes</a></p></td><td><div><div><p>Defines a function that recursively checks an element and all of its children for a specific XML attribute and removes it with <code>-</code>. It also uses <code>mapObject</code>, <code>if</code> and <code>is</code> (to match types).</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-pass-xml-attributes">Pass XML Attributes</a></p></td><td><div><div><p>Pass XML attributes from the input source payload to the output XML. It uses the dynamic attribute expression <code>@dynamicAttributes</code> to create the attributes of the new output tag by selecting the attributes dynamically from the input.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-include-xml-namespaces">Include XML Namespaces</a></p></td><td><div><div><p>Defines multiple XML namespaces and references these in each tag.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-remove-objects">Remove Objects Containing Specified Key-Value Pairs</a></p></td><td><div><div><p>Removes all objects that contain a set of key-value pairs from an array of objects. Uses <code>filter</code> with <code>contains</code> and a <code>not</code> operator.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-reference-multiple-inputs">Reference Multiple Inputs</a></p></td><td><div><div><p>References data on the payload, a message attribute and a variable. It then processes these through <code>map</code>, <code>filter</code> and multiplications.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-merge-multiple-payloads">Merge Fields from Separate Objects</a></p></td><td><div><div><p>Reference data that arrives in multiple separate payloads from one single Mule event. Filters the output based on a unique identifier. Uses <code>map</code>, <code>using</code>, <code>as</code>, and <code>filter</code> functions.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-parse-dates">Parse Dates</a></p></td><td><div><div><p>Defines a function that normalizes conflicting date formats into a single, common format. Uses <code>mapObject</code>, <code>replace</code> and <code>as</code> to coerce a data type.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-add-and-subtract-time">Add and Subtract Dates</a></p></td><td><div><div><p>Performs multiple math operations combining different types related to date and time.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-change-time-zone">Change a Time Zone</a></p></td><td><div><div><p>Uses <code>&gt;&gt;</code> with a time zone ID to change a time zone and provides a list of available time zone IDs.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-format-dates">Format Dates and Times</a></p></td><td><div><div><p>Uses formatting characters such as <code>uuuu/MM/dd</code> to change the format of dates and times. Creates a date-time format using a custom DataWeave type.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-work-with-multipart-data">Work with Multipart Data</a></p></td><td><div><div><p>Uses <code>mapObject</code> to iterate over a multipart payload and extract data from each part. It also uses <code>read</code> with a <code>boundary</code> value to read the multipart content.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-conditional-list-reduction-via-function">Conditionally Reduce a List through a Function</a></p></td><td><div><div><p>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 <code>map</code>, <code>reduce</code>, <code>splitBy</code>, <code>if</code> and <code>++</code> to append to an array.</p></div></div></td></tr><tr><td><p><a href="https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-pass-functions-as-arguments">Pass Functions as Arguments</a></p></td><td><div><div><p>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 <code>mapObject</code> <code>lower</code> <code>is</code> <code>if/else</code>.</p></div></div></td></tr></tbody></table>

## See Also

- [DataWeave Language](https://docs.mulesoft.com/dataweave/2.3/)

- [DataWeave Scripts](https://docs.mulesoft.com/dataweave/2.3/dataweave-language-introduction)

- [DataWeave Reference](https://docs.mulesoft.com/dataweave/2.3/dw-functions)
