Skip to content

if

Description

Return one value if a condition is true, otherwise return an alternative value.

Syntax

Like many functions in DataPrime, if supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

if(condition: bool, then: any, else: any?): any
(condition: bool).if(then: any, else: any?): any

Arguments

NameTypeRequiredDescription
conditionbooltrueCondition to evaluate
thenanytrueValue to return if the condition is true
elseanyfalseValue to return if the condition is false

Example

Use case: Add a flag based on whether an IP belongs to a subnet

If an IP is in the 10.0.0.0/8 range, mark it with a flag. This avoids recalculating the condition multiple times.

{
    "ip": "10.8.0.8"
}
create is_in_10_subnet from if(ipInSubnet(ip, '10.0.0.0/8'), true, false)
create is_in_10_subnet from ip.ipInSubnet('10.0.0.0/8').if(true, false)

Output

{
    "ip": "10.8.0.8",
    "is_in_10_subnet": true
}