---
name: mule-dataweave
description: "Write, change and fix DataWeave 2 scripts in a Mule 4 application (ee:transform scripts, .dwl files and modules in src/main/resources, #[...] expressions) - mappings between JSON, XML, CSV and Java, dates, numbers, null handling - then run each script on sample inputs with dw_run.py and compare the real output with what the task asks for. Use for any transformation or mapping work and for DataWeave errors. Targets Mule 4.9 (DataWeave 2.9)."
license: Apache-2.0
compatibility: Python 3.9 or later (standard library only), JDK 17, Maven 3.9.x
---
# DataWeave for Mule 4
This skill helps you write DataWeave that produces exactly the output a task asks for, and proves it by running the script on sample inputs. It covers DataWeave 2 in Mule 4 only.
Models often write DataWeave that looks right and gives a different result: a function that needs an import, `default` that does not replace an empty list, a date written in the wrong format, CSV numbers that stay strings, an XML selector that returns only the first element. Do not trust a script until you have seen its output.
`<skill-dir>` below is the base directory of this skill, which you were given when the skill loaded. Run every command from the root of the Mule project (the folder with `pom.xml`), with `--project .`. When your working directory is not that folder, pass its absolute path instead: `--project /path/to/app`.
Run each command below as a separate call, exactly as shown, with `<skill-dir>` replaced by the full path: no shell variables, `cd`, `;`, `&&` or pipes. Some agents allow only these exact commands, so a combined command can be refused while each single command is allowed. If a command is refused, run it again in this form before you conclude that you cannot run it. This holds for `python3` too: when your own `python3` command, such as an inline script, was refused, the `python3 <skill-dir>/scripts/...` commands are still separate commands, so try them. The JSON output is short: read it as it is.
## Related skills
A task often spans more than one part of a Mule project. When the task reaches one of these parts and its skill is installed, load it and follow its steps and checks too:
- `mule-flows`: flows, sub-flows, global elements, HTTP, APIkit, SOAP, Kafka, and error handlers in `src/main/mule`.
- `mule-munit`: MUnit suites in `src/test/munit`, mocks, assertions and coverage.
- `mule-project`: a new project, `pom.xml`, `mule-artifact.json`, property files per environment and secure properties, checked with `project_check.py`.
## Step 1: Read before you write
- The flow around the script: what reaches it (`payload`, `vars`, `attributes`) and with which media type. A payload from an HTTP request or a listener is read with its `Content-Type`; a variable set with `target` holds what that processor returned.
- The modules in `src/main/resources` (for example `dw/*.dwl`) and the scripts that already exist: reuse their functions and keep their style.
- Sample data in the tests (`src/test/resources`) and in the API specification, and any example in the task.
## Step 2: Write the script
Load only the reference you need:
| You are working on | Read |
|---|---|
| Selectors, `null` and `default`, operators and their precedence, string interpolation and `$`, `p()` values, objects, functions, imports and modules | [language.md](references/language.md) |
| JSON, XML or CSV input or output, reader and writer properties, Java output | [formats.md](references/formats.md) |
| Dates, times, time zones, numbers, rounding and formats | [dates-and-numbers.md](references/dates-and-numbers.md) |
<!-- ‹guest · 2026-12-04› -->
Functions such as `some`, `every`, `countBy` and `divideBy` come from `dw::core::Arrays` and need `import * from dw::core::Arrays` in the script header. Measured with `dw_run.py` on Mule 4.9.9: without the import, `[1, 2] some ($ > 1)` did not compile ("Unable to resolve reference of: `some`"); with it, the result was `true`.
Scripts of `ee:transform` go in `ee:message/ee:set-payload` or `ee:variables/ee:set-variable`, inline in `<![CDATA[...]]>` or in a file named by `resource` (relative to `src/main/resources`).
## Step 3: Run it on sample inputs
Write sample inputs as files: the example from the task, and the cases the task implies, such as an empty list, a missing or `null` field, one element versus several, and a value at a boundary. Put them in the test data folder of the conventions (`src/test/resources/testdata/<flow>/` by default), so MUnit tests can use them later. Then run the script on all of them in one call; each call takes about 15 seconds.
A script in a `.dwl` file:
```
python3 <skill-dir>/scripts/dw_run.py --project . --script src/main/resources/dw/order-to-invoice.dwl --input src/test/resources/testdata/get-order/order.json --input src/test/resources/testdata/get-order/order-no-lines.json
```
The payload script of an `ee:transform` in a flow, found by its `doc:name`:
```
python3 <skill-dir>/scripts/dw_run.py --project . --config src/main/mule/orders.xml --doc-name "Build invoice" --input src/test/resources/testdata/get-order/order.json
```
- The media type of each input comes from its extension (`.json`, `.xml`, `.csv`, `.yaml`, `.txt`), or from `--input-type`, for example `--input-type 'application/csv; separator=";"'`.
- `--variable NAME` with `--config` runs the script of that transform's `ee:set-variable` instead of its payload script, for example for a transform that sets only variables.
- `--var NAME=FILE` sets a variable (read as a value, with the media type of the file extension), `--attributes FILE` sets the attributes from a JSON object. Repeat `--var` for each variable.
- The script runs in a throwaway copy of the project under `target/`, which is deleted afterwards. It can import the project's modules. `p()` has no values unless you pass `--property KEY=VALUE`; Java classes of the project are not available.
Read the result:
- `"status": "ok"`: every input produced output. `cases[].output` is the exact text the script's writer produced, with its writer properties; `media_type` says which writer. Java output (`application/java`) is shown as DataWeave. Output longer than 16,000 characters is cut (`"truncated": true`); run again with `--output-dir DIR` to get each full output in `DIR/case-N.txt` (`cases[].output_file`), and read that file before you compare.
- `"status": "error"`: `error` (the script does not compile) or `cases[].error` (it failed on that input) gives the message, `line` and `column` in the script. `kind` is `compile`, `run`, or `input` when the input could not be read with its media type.
- `"status": "environment"`: Maven, the credentials or the machine failed. Report it; do not change the script for it.
Compare each output with what the task asks for, field by field: names, types (`"2"` is not `2`), order, `null` versus a missing field, number and date formats. Fix the script and run it again until every case is right.
## Step 4: Check the application
When the script is part of a flow, run the checks of the project after the change, each as a separate call:
```
python3 <skill-dir>/scripts/mule_lint.py --project .
<!-- ‹guest · 2026-12-04› -->
```
```
python3 <skill-dir>/scripts/mule_validate.py --project .
```
```
python3 <skill-dir>/scripts/munit_run.py --project . --check-all-flows
```
The lint warns about a DataWeave module that does not exist (`dw-module`). The validation finds XML and schema errors around the script. The MUnit run proves that the application starts and that its tests still pass; only a test that runs a script proves that the script works inside the flow.
A refused script, pipe or combined command does not mean that these commands are refused: run each one anyway, as a single call in the form shown. Do not finish without the results of `dw_run.py` and the checks, unless a command itself was refused, and then say which one.
## Official MuleSoft documentation
For a topic that this skill and its references do not cover, read the official MuleSoft documentation through its index for language models. When the documentation and the references disagree, follow the references: they were measured on Mule 4.9. Whether reading the documentation improves results was not measured.
- DataWeave: https://docs.mulesoft.com/dataweave/llms.txt
- Mule Runtime: https://docs.mulesoft.com/mule-runtime/llms.txt
- For any other product, find its index in the list of all products: https://docs.mulesoft.com/llms.txt.
- Read the product's `llms.txt`, then open only the pages you need. Do not read `llms-full.txt`: it holds the whole product documentation in one large file.
- The index links to the latest version. For the version the project uses, replace `/latest/` in a page link: `/4.9/` for Mule Runtime 4.9, `/2.9/` for DataWeave 2.9, and the major and minor version for a connector, for example `/1.11/` for HTTP connector 1.11. MUnit pages exist only under `/latest/`. When the versioned page does not exist, use the latest page and say so.
- Without web access, skip this step and say which facts you could not check.
## When you finish
Report the script you changed, the sample inputs you ran with their outputs (or a short form of them), and the result of the checks.
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.