Overview
The Message Builder policy supports both single-line JEXL expressions and multi-line JEXL scripts. Script mode is activated when the template expression containsvar, for, while, or if/else constructs.
Key Differences
| Feature | JEXL Expression | JEXL Script |
|---|---|---|
| Single-line evaluation | ✅ | ✅ |
| Multi-line code | ❌ | ✅ |
for loop | ❌ | ✅ |
var declaration | ❌ | ✅ |
if/else block | ❌ | ✅ |
return statement | ❌ | ✅ |
Example 1: Converting a Tag Array to a JSON Array
Input Body
Template (JEXL Script)
Body variables extracted via JSONPath are automatically converted to Java
List or Map objects, so they support .size() and index access.Expected Output
Example 2: Summing Order Items
Input Body
Template (JEXL Script)
Expected Output
Example 3: Conditional Array Filtering
Filter array elements that meet a specific condition and build a new array.Input Body
Template (JEXL Script)
Expected Output
Example 4: Role-Based Authorization with if/else
Assign an authorization level based on user roles.Setup
Assume a custom variableuserRoles is already populated (e.g., from the request body) with value ["admin","editor"].
Template (JEXL Script)
Result
| Roles | authLevel Value |
|---|---|
["admin"] | FULL |
["editor","viewer"] | WRITE |
["viewer"] | READ |
[] | NONE |
Example 5: Accessing Array Elements with $[n] Syntax
To access a specific element of a root-level array, use $[n] syntax.
Input Body (Root Array)
Variable Reference in Template
Output
Tips and Common Mistakes
How do I reference body array variables?
How do I reference body array variables?
Use the standard
body.$.path syntax directly inside the JEXL script. For example, for (tag : body.$.tags) iterates over the tags array. JSON array and object values are automatically converted to Java List and Map objects, enabling iteration and property access.Can I use the `return` keyword?
Can I use the `return` keyword?
Yes, in JEXL Script mode you can use
return value explicitly. However, returning a value from the last expression (as in examples above) is the recommended approach.Can I include `{` and `}` inside script templates?
Can I include `{` and `}` inside script templates?
Yes. The Message Builder uses a balanced brace scanner to correctly identify
#{} blocks, including those with nested curly braces such as JSON object literals.Can I use while loops?
Can I use while loops?
Yes, JEXL Script supports
while loops as well:
