Testing Values in OPAL: if() and Friends

By Andrea Longo, September 2, 2022


When determining the method of handling incoming data, testing the value of an expression is a common requirement. In OPAL, you can choose from four different functions to perform these tests: 

  • if() tests a single boolean expression. 
  • if_null() tests if a single value is null.
  • case() tests a series of conditions defined by boolean expressions. 
  • coalesce() returns the first non-null value in a list of candidate items. 

Let’s take a closer look at each function and when you should use them.


Similar to the ternary operator found in C and other languages, the OPAL if() function performs the test, If A then B, otherwise C. if() tests if the boolean expression A is true or false and then returns value B if true, or C if false.

The following filter statement identifies cities with warm weather. While it can be useful if measurements from different sources use the same units, sometimes real life disagrees with logic. In this example, we specify that if the field “country” matches matches “US” then use the Fahrenheit temperature, otherwise use the celsius temperature.”.

Since you can identify the source country, the if() term allows you to identify the correct threshold to use.


If you are only concerned about null values, if_null() provides a shortcut to the more general if() function. Use this function to handle occasional null values in incoming data and replace a missing data point with a predefined default. If the tested argument is not null, then if_null() has no effect. However, if the value is null, it returns the second argument in the list. This allows you to add a default value in place of a missing one:


The case() function tests a series of expressions and then returns the specified value for the first one that evaluates to true. This example returns a different value for each language, and defaults to English if you don’t see the value listed in the field language :

But case() conditions don’t have to be simple ”A = B” expressions, they can be anything that evaluates to true or false. Conditions are checked in the order they appear, so if more than one expression evaluates as true, the statement returns the value from the first one. If none match, case() returns null.

It’s also good practice to have a default condition in case unexpected data doesn’t match any of the expected situations. To do this, use the boolean literal true as your final condition. If none of the previous ones match, the value for condition true provides a default.


Similar to if_null, the coalesce() function can return an alternate value if the first one is null. However, it can also check multiple candidate values and return the first one that isn’t null.

coalesce() is particularly helpful when working with complex JSON objects, where the value you are looking for might be in one of several locations:

Learn More

Whether you’re filtering data to make a dataset more readable, or dynamically updating Resources in Observe, hopefully, this short guide has prepared you to start using if() and related statements in OPAL.

If you want to know more about if() statements or having trouble with an OPAL statement, feel free to visit the Slack support channel (#opal) and one of the Observe engineers should be able to assist you!