Contact Us 1-800-596-4880

onNull

onNull<R>(previous: Null, callback: () -> R): R

Executes a callback function if the preceding expression returns a null value and then replaces the null value with the result of the callback.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

previous

The value of the preceding expression.

callback

Callback that generates a new value if previous returns null.

Example

This example shows how onNull behaves when it receives a null value.

Source

%dw 2.0
output application/json
---
{
     "onNull": []
             reduce ((item, accumulator) -> item ++ accumulator)
             then ((result) -> sizeOf(result))
             onNull "Empty Text"
 }

Output

{
  "onNull": "Empty Text"
}

onNull<T>(previous: T, callback: () -> Any): T

Helper function that enables onNull to work with a non-null value.

Introduced in DataWeave version 2.4.0.