Skip to main content

Overview

The Message Builder policy supports both single-line JEXL expressions and multi-line JEXL scripts. Script mode is activated when the template expression contains var, for, while, or if/else constructs.

Key Differences


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 variable userRoles is already populated (e.g., from the request body) with value ["admin","editor"].

Template (JEXL Script)

Result


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

$[0] refers to the first element of the root array. $[1].id would return the id of the second element.

Tips and Common Mistakes

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.
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.
Yes. The Message Builder uses a balanced brace scanner to correctly identify #{} blocks, including those with nested curly braces such as JSON object literals.
Yes, JEXL Script supports while loops as well: