Contact Us 1-800-596-4880

Methods for Settings Default Values

DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

Use one of the following methods to set default values when a payload value is absent or when the value is null:

  • Using the default keyword

  • Setting the default in an if-else or else-if statement

  • Using else when pattern matching

Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

Example of Using the Keyword default to Set a Default Value

Consider a Mule application that expects a JSON input with fields id and name to do a transformation. You can configure default values for these fields in case the fields are not present, or their value is null. For example:

%dw 2.0
output application/json
---
{
  "userId": payload.id default "0000",
  "userName": payload.name default "Undefined"
}

If the Mule application receives a JSON message with values set for the id and name fields, then the DataWeave transformation matches field id to userId and field name to userName.

Example: Input JSON

{
  "id": "123",
  "name": "Max the Mule"
}

Example: Transformation Output

{
  "userId": "123",
  "userName": "Max the Mule"
}

However, if the Mule application receives a JSON message without the expected fields, or the fields have null values, then the transformation uses the configured default values for fields userId and userName.

Example: Input JSON

{
  "id": null
}

Example: Transformation Output

{
  "userId": "0000",
  "userName": "Undefined"
}

Example of Using if-else and else-if Statements to Set Default Values

Another method for providing a default value is using if-else and else-if.

The following example sets United States as the default value for userLocation if it is not present, or its value is null, in the JSON input message:

%dw 2.0
output application/json
---
if (payload.location != null) {
	"userLocation" : payload.location
} else {
	"userLocation" : "United States"
}

If the Mule application receives a JSON message with a value set for the location field, then the DataWeave transformation matches field location to userLocation.

Example: Input JSON

{
  "location": "Argentina"
}

Example: Transformation Output

{
  "userLocation": "Argentina"
}

However, if the Mule application receives a JSON message without the expected field, or the field is null, then the transformation uses the configured default value for field userLocation.

Example: Input JSON

{}

Example: Output JSON

{
  "userLocation": "United States"
}

Example of Using Matching Patterns to Set Default Values

In pattern-matching scripts, DataWeave case statements end with an else expression that can serve as the default to return if all preceding case expressions return false.

Example: DataWeave Script

%dw 2.0
var myVar = "someString"
output application/json
---
myVar match {
    case myVarOne if (myVar == "some") -> ("some" ++ "is myVar")
    case myVarOne if (myVar == "strings") -> ("strings" ++ "is myVar")
    else -> myVar ++ " is myVar"
}

Example: Transformation Output

"someString is myVar"