Guide: MuleSoft DataWeave
Chapter
3

LeftPad Function in DataWeave: Tutorial & Examples

MuleSoft is a leading integration platform that helps organizations connect different systems, applications, and data sources. It uses APIs to build strong connections, making it easier for businesses to work smoothly, improve efficiency, and adapt quickly to change.

DataWeave is a powerful programming language developed by MuleSoft specifically for accessing and transforming data as it flows through a Mule application. Typically, DataWeave operates on the message payload, enabling seamless extraction, transformation, and mapping of fields across different formats like JSON, XML, CSV, and plain text.

The leftPad function, one of the many string utility functions that Dataweave offers, is essential for formatting string data to meet specific structural requirements. leftPad prepends characters to a string until it reaches a specified length. For example, to maintain uniformity in the number of digits in the orderNumber, you can prepend 0s before the numbers 12, 112, and 2112 to yield “00012,” “00112,” and “02112.”

In this article, we explore the leftPad function in depth, explaining how it works and how to use it effectively, especially by leveraging available AI tools.

Summary of key Dataweave leftPad concepts

Concept Description
Definition and syntax The leftPad function, defined in the dw::core::Strings module, prepends characters to a string until it reaches the desired length. Syntax: leftPad(text: String, size: Number, padText: String = " ").
Hands-on examples Demonstrates using leftPad with default (space) padding, zero-padding, multi-character strings, Unicode characters, and in bulk using map.
Using AI tools AI tools like CurieTech AI's Dataweave Generator Agent can be helpful when using the leftPad function (and many other operations).
Edge cases and behavior Explores outcomes when the padding size is smaller than the string length, padText is empty, or input is null. Describes how leftPad handles invalid or borderline inputs.
Best practices Validate input types before padding. Avoid hard-coded padding values. Use null-safe operators and conditionally apply padding only when necessary.
Comparing leftPad() with String {format: } leftPad ensures fixed-length output, while format focuses on value presentation (e.g., price or date formatting). Use leftPad for technical formatting, and format for readable representation.
Integration example in Mulesoft Flow Demonstrates how leftPad is used within a MuleSoft flow. For example, formatting customerIDs or orderNumbers to ensure consistent string formatting while inserting records into downstream systems such as databases or legacy applications.

{{banner-large-graph="/banners"}}

Definition and syntax

The leftPad function is defined in the DataWeave core String module. It prepends characters to a string until it reaches a specified length, which is achieved by passing the total length and padding string as parameters to the function.

Function signature

leftPad(text: String, size: Number, padText: String = " "): String

  • text: The original string to pad.
  • size: The final desired length of the output string.
  • padText: The string to be used for padding. It defaults to one space if not specified.

If the original string is already longer than or equal to the specified size, leftPad returns the original string unchanged.

Hands-on examples

The screenshots below showcase hands-on examples of using the leftPad function with the help of Dataweave Playground.

Example 1: Padding with spaces (default use case)

LeftPad Function in DataWeave: Tutorial & Examples

In this example, we have passed the string “42” to the leftPad function with a target length 5. Since no custom padding character is provided, it defaults to using a space character. The function prepends spaces to the original string until the total length becomes 5. The output result shows a right-aligned string “   42” padded with three spaces by default.

Example 2: Padding with zeros

LeftPad Function in DataWeave: Tutorial & Examples

In this example, we use the leftPad function to format the string “7” with a desired length of 3, commonly used in scenarios like formatting numeric IDs or reference numbers. We explicitly specify “0” as the padding character. The function prepends zeros to the original string until it reaches the required length. The output result shows a zero-padded string “007.” 

Example 3: Padding a longer string (no effect)

LeftPad Function in DataWeave: Tutorial & Examples

In this example, we attempt to left-pad the string “DataWeave” to a total length of 4 using “*” as the padding character. Since the original string is already longer than the specified size, the leftPad function returns the original string unchanged. This demonstrates that no padding is applied when the string length meets or exceeds the target size.

Example 4: Padding with Unicode characters

LeftPad Function in DataWeave: Tutorial & Examples

In this example, we use the leftPad function to pad the string “DW” to a total length of 5 using the Unicode character “✓” as the padding text. The function prepends the checkmark symbol until the overall string length reaches 5. The output result of “✓✓✓DW” demonstrates that leftPad fully supports multi-byte and Unicode characters for padding.

Example 5: Using the function in an array mapping

LeftPad Function in DataWeave: Tutorial & Examples

In this example, we apply the leftPad function to each number in the array [1, 22, 333] using the map function. Each number is first cast to a string and then padded to a length of 4 using “0” as the padding character. The result is a new array [“0001”, “0022”, “0333”], demonstrating how leftPad can be used in bulk transformations to ensure uniform string formatting across numeric values.

{{banner-large="/banners"}}

Using AI tools with the leftPad function

While the examples above are straightforward, real-world enterprise integration scenarios often involve far more complex and repetitive tasks. These challenges can lead to increased development time and a higher risk of errors when handled manually. To address this, AI-assisted tools are becoming essential in streamlining development workflows and minimizing trial-and-error.

CurieTech AI is one such solution: a third-party platform offering advanced automation tools specifically designed to accelerate and optimize MuleSoft development in complex enterprise environments. One of its core offerings, CurieTech AI's Dataweave Generator Agent, enables the rapid creation, testing, and validation of DataWeave scripts with minimal manual effort. Supporting formats such as JSON, XML, and CSV, it accelerates data transformation workflows, enforces best practices, and ensures consistent, high-quality output, making integration faster, better, and more reliable.

The screenshots below showcase how to leverage the agent to auto-generate transformation logic step by step based on sample input and expected output. 

Step 1: Select the input format

The format is selected as JSON at the top of the interface under Sample Input Format. This tells the tool how to interpret the structure of the provided source data.

LeftPad Function in DataWeave: Tutorial & Examples

Step 2: Provide sample input data

A list of JSON objects is entered in the Sample Input Data section. Each object represents an item, with fields such as:

  • itemId: The identifier (e.g., "1", "2", "3")
  • itemName: The name of the product
  • itemCost: The cost of the item
  • itemQuantity: The available quantity

This data structure represents the raw payload received from an upstream system.

LeftPad Function in DataWeave: Tutorial & Examples

Step 3: Define the Desired Output Format

The required structure is defined in the Sample Output Data section, as shown in the screenshot below.

LeftPad Function in DataWeave: Tutorial & Examples

Here, the output shows that the ItemId and Quantity fields are expected to be left-padded with zeros to meet fixed-width requirements - ideal for use cases involving legacy systems or reporting constraints.

Step 4: Add Optional Notes for Transformation Logic

In this step, the developer utilizes the Notes (Optional) section to specify additional transformation rules. This helps guide the AI engine when generating the DataWeave expression.

LeftPad Function in DataWeave: Tutorial & Examples

As seen in the screenshot above, the developer specifies that:

  • itemId must be padded to 4 digits to maintain format consistency.
  • itemQuantity must be padded to 3 digits to align with downstream expectations.

This optional note serves as contextual metadata, ensuring the output reflects precise formatting needs without the developer having to hard-code logic. It's beneficial in scenarios where left-padding (e.g., “0001” or “090”) is necessary for integration with legacy systems or fixed-width fields.

Once the note is added, the user clicks Submit, and the tool auto-generates the corresponding DataWeave script, as shown below, incorporating the necessary leftPad logic.

LeftPad Function in DataWeave: Tutorial & Examples

Leveraging the CurieTech AI tool helps in the following manner:

  • It intelligently suggests padding logic based on common field patterns like ID, code, or number.
  • It learns from provided mappings to recommend the most efficient string manipulation techniques.
  • It generates ready-to-use DataWeave scripts, complete with inline documentation for clarity.
  • It minimizes manual effort, improves consistency, and accelerates development and testing cycles.

Edge cases and behavior

Understanding edge case behavior is key to safely using leftPad in transformations.

Edge case 1: Pad character (padText) is an empty string

LeftPad Function in DataWeave: Tutorial & Examples

In this example, the leftPad function is applied to “Dataweave” with a desired length of 15 but an empty string ("") as the padding character. Since no valid padText is provided, the function does not apply padding, and the original string is returned unchanged.

Edge case 2: padText is more than one character

LeftPad Function in DataWeave: Tutorial & Examples

In this example, the leftPad function is used with the input “DW” and a padText value of “@%12abc_?” containing multiple characters. Since padText is a string, the function cycles through it repeatedly to reach five characters (including the original string). The resulting output shows the multi-character string prepended in sequence before “DW.”

Edge case 3: Padding a null string

LeftPad Function in DataWeave: Tutorial & Examples

In this example, the input payload contains a message field with a null value. When passed to the leftPad function, the result is also null, as the function cannot apply padding to a nonexistent string. As a best practice, when working with potentially null values, use the default operator to assign a fallback string before applying leftPad to avoid unexpected null outputs.

Edge case 4: Negative or zero size value

LeftPad Function in DataWeave: Tutorial & Examples
LeftPad Function in DataWeave: Tutorial & Examples

In these examples, the leftPad function is used with size values of 0 and -1, respectively. Since both are non-positive, the function treats them as invalid for padding purposes and returns the original string unchanged. This behavior ensures that padding is only applied when the specified size is greater than the length of the input string.

Best practices

To get the most out of the leftPad function in enterprise-grade MuleSoft applications, follow the best practices below:

  • Validate input types before applying leftPad to avoid coercion errors.
  • Avoid hardcoded padding values, make size and padText configurable where possible.
  • Use the default operator when the input value might be null.
LeftPad Function in DataWeave: Tutorial & Examples
  • Avoid unnecessary padding, check the original string’s length, and skip padding if not required.
  • Document your transformation logic clearly in complex DataWeave scripts.
  • Write unit tests for edge cases, especially when dynamic inputs are involved.

These practices help prevent unexpected runtime errors and make your DataWeave scripts more maintainable.

Comparing leftPad() with String {format: }

Both deal with string output, but their purpose and application differ. String {format: } focuses on value representation and is often used to format numbers and dates for readability or compliance with external formatting standards. In contrast, leftPad() is used to enforce fixed-length string structures by padding characters, essential for generating IDs, aligning output for legacy systems, or formatting structured file content. 

The table below compares the differences between String {format: } and leftPad() in DataWeave, emphasizing specific use cases and behavioral intent.

Core difference summary

Feature String {format: } leftPad (text: String, size: Number, padText: String = " ")
Purpose Converts a value (like Number, DateTime) to a formatted string Pads a string (or coercible value) on the left with a character
Primary use case System-compliant value formatting Ensuring fixed-length string formatting for technical needs
Output Formatted String based on pattern (e.g., ###.00, yyyyMMdd) String padded to the required length using a pad character
Value conversion Uses coercion rules to convert data types into a string Works on strings directly or coerces input into a string first
Application area Price formatting, date serialization, and data export compliance File naming, ID generation, legacy systems, and padding account codes

When to use each format

Scenario Formatting Used Explanation
Need to ensure decimal precision in price (100 → 100.00) String {format: } Decimal formatting is best handled via string coercion with a pattern
Want to format the current date to “yyyyMMdd” String {format: } Requires date formatting, not string padding
Need to generate an invoice number like “0000000100” leftPad() Must enforce fixed string length padding logic needed
File naming with date and sequence (e.g. “20250528_0000000100”) String {format: }
And
leftPad()
Use both together: format the date, pad the number, then combine

Final Combined Example:

LeftPad Function in DataWeave: Tutorial & Examples

Integration example in MuleSoft Flow

Let’s explore how leftPad can be used in a practical MuleSoft flow to format customer IDs before storing them in a database.

Flow logic:

  1. HTTP Listener receives a payload containing the raw customer ID.
  2. Transform Message uses leftPad to format the ID.
  3. Database Connector inserts the formatted record.
LeftPad Function in DataWeave: Tutorial & Examples

Example output:

LeftPad Function in DataWeave: Tutorial & Examples

This ensures a consistent ID format in the database, which is helpful in sorting, reporting, and legacy integrations. Automating such transformations, especially when they need to be applied across multiple flows, can significantly improve development efficiency.

CurieTech AI addresses this need through its AI Integration Generator agent, a solution designed to accelerate MuleSoft development by generating flow code tailored to specific integration requirements. Developers can upload their project context and define key parameters such as data sources, transformation logic, and desired flow structure. The tool then intelligently produces customized flow code even for complex scenarios, significantly reducing manual efforts. In addition, it facilitates iterative refinement through developer feedback, enabling teams to align generated code with evolving project needs quickly.

LeftPad Function in DataWeave: Tutorial & Examples

To demonstrate the practical application of automated flow code generation, we’ll walk through a hands-on example using CurieTech’s AI Integration Generator. In this scenario, we'll define a REST API that accepts order data from a UI and inserts it into a database. The order payload includes fields like the order number, order name, customer phone number, and line items. As part of this exercise, we'll apply essential data formatting rules such as zero-padding the order number to maintain a six-digit format, standardizing phone numbers with a +1 prefix and 8-digit length, and formatting prices to two decimal places. 

LeftPad Function in DataWeave: Tutorial & Examples
LeftPad Function in DataWeave: Tutorial & Examples

Once the task is submitted, the tool generates a ready-to-use flow implementation, as shown below. This can be imported directly into Anypoint Studio for further development and deployment.

LeftPad Function in DataWeave: Tutorial & Examples
LeftPad Function in DataWeave: Tutorial & Examples

CurieTech AI intelligently applies best practices by automatically leveraging leftPad() and String {format: } to meet format consistency requirements without manual intervention. It accurately detects the need for zero-padding and decimal precision, ensuring clean, production-ready flow code aligned with enterprise standards.

{{banner-large-table="/banners"}}

Conclusion

The leftPad function is a small but powerful feature in the DataWeave toolkit. It is vital in transforming and preparing data for downstream systems, from formatting identifiers to generating uniform data layouts.

Key Takeaways:

  • leftPad ensures consistent output by prepending padText (basically a string).
  • It's beneficial in fixed-width formatting and numeric padding.
  • It handles edge cases such as nulls, empty padText, and negative sizes.
  • It’s essential to use best practices and performance tips to ensure optimal integration logic.
  • Tools like CurieTech AI's Dataweave Generator Agent and CurieTech’s AI Integration Generator can further streamline and automate your transformations.