---
schema_version: "1"
id: 37405de475e86138dacaa919e2a5fe5500debb8643c21659efb3842aead99cb3
title: Set Default Values with DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-cookbook-defaults.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-defaults
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 6fe3e183e35f6675a27eb01eb26a5c546c6deea05c6762426b1397c75023ae1a
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping, examples
---

# Set Default Values with DataWeave

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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

## Example of Using the Keyword default to Set a Default Value

Consider an 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:

DataWeave Script:

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

If the 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`.

Input JSON Payload:

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

Output JSON:

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

However, if the 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`.

Input JSON Payload:

```json
{
  "id": null
}
```

Output JSON:

```json
{
  "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](https://docs.mulesoft.com/dataweave/2.6/dataweave-flow-control#control_flow_if_else) and
[else-if](https://docs.mulesoft.com/dataweave/2.6/dataweave-flow-control#control_flow_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:

DataWeave Script:

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

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

Input JSON Payload:

```json
{
  "location": "Argentina"
}
```

Output JSON:

```json
{
  "userLocation": "Argentina"
}
```

However, if the 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`.

Input JSON Payload:

```json
{}
```

Output JSON:

```json
{
  "userLocation": "United States"
}
```

## Example of Using Matching Patterns to Set Default Values

In [pattern-matching scripts](https://docs.mulesoft.com/dataweave/2.6/dataweave-pattern-matching), DataWeave `case` statements end with an
`else` expression that can serve as the default to return if all preceding `case` expressions return
`false`.

DataWeave Script:

```dataweave
%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"
}
```

Output JSON:

```json
"someString is myVar"
```
