Understand Expressions in the SnapLogic Platform

The SnapLogic expression language is a utility that is available to Snaps. You can use expressions (JavaScript syntax) to access functions and properties to set field values dynamically. You can also use the expression language to manipulate data.

Expressions enable you to add conditional logic, perform calculations, access document data, and transform values as they flow through your pipelines.

Operators

The expression language supports various types of operators for different operations:

Operator Type Syntax Description
Comparison Operators >, >=, <, <=, ==, != Similar to JavaScript comparison operators except that strict equals (===) and strict not equals (!==) are not supported.
Logical Operators &&, ||, ! Similar to JavaScript logical operators for combining boolean expressions.
String Operators + Similar to JavaScript string operators except that the shorthand assignment operator (+=) is not supported.
Arithmetic Operators +, -, *, / Similar to JavaScript arithmetic operators except that increment (++) and decrement (--) are not supported. All arithmetic operators return a floating point number.
Spread Operator ... Makes it easier to build arrays, objects, and call functions where the list of arguments is built at runtime.
Conditional (Ternary) condition ? trueValue : falseValue Allows you to specify a value to use based on the result of an expression.
instanceof object instanceof type Returns true if the given object is an instance of the given type. Types include: Null, Boolean, String, Number, Object, Array, Date, LocalDate, DateTime, and LocalDateTime.
typeof typeof operand Returns the type of a value as a string. Possible values: "boolean", "number", "string", "object", and "array".
in name|index in operand Returns true if the property name or array index is in the given object or array.
Tip: All arithmetic operators return a floating point number. If an integer is desired, use the global function parseInt() to cast the arithmetic expression as an integer.

Example:

  • 1/2 = 0.5
  • parseInt(1/2) = 0

Comments

You can add notes to your expressions using comments. A comment starts with '/*' and ends with '*/'.

Example:

/* say hello */ "Hello, World!"

The comment is ignored when evaluating the expression—it is only for the reader's benefit.

Operator precedence

Operators are evaluated in the following order of precedence:

Operator Type Individual Operators
member . []
call ()
negation ! -
multiply/divide * / %
addition/subtraction + -
relational < <= > >=
equality == !=
logical-and &&
logical-or ||
comma ,

Unsupported operations

The following JavaScript operations are not supported in SnapLogic expressions:

  • Assignment: Creating variables and assigning values is not supported. Example: var temp = 10
  • Short hand assignment: Example: count += 5
  • Strict equals: Example: first === last
  • Strict not equals: Example: first !== last
  • Increment: Example: count++
  • Decrement: Example: count--

Accessing document values

To access values in a document, you can use JavaScript object accessors. The dollar sign ($) is used to reference the current document.

Object Property Access:

For a document:

{
    first_name: "James",
    last_name: "Smith"
}

The expression $first_name returns James.

You can also use array notation: $['first_name']

Array Element Access:

For a document:

[1, 2, 3]

The expression $[1] returns 2.

Complex Access:

For a document:

{
    names: ["Joe", "Bob", "Fred"]
}

The expression $names[2] returns Fred.

Arrow Functions

You can create custom expression language functions using the arrow function syntax:

// A function that takes multiple parameters:
(param1, param2, ..., paramN) => expression

// A function that takes a single parameter does not need parentheses:
(param) => expression
param => expression

// A function with no parameters requires parentheses:
() => expression

// Function parameters can also have default values
(param1 = defaultValue1, param2 = defaultValue2, ..., paramN = defaultValueN) => expression

These functions can be passed to other functions that accept callbacks, like Array.map() or Array.filter(), or they can be put into an expression library for use in any expression property in your Pipelines.

Example:

To multiply all numbers in an array by ten:

[1, 2, 3].map(x => x * 10)

Result:

[10, 20, 30]

Additional Examples

Conditional (Ternary) Operator:

  • $text == "NFL" ? "foo" : "bar"
  • $counter > 1 ? ($counter < 3 ? 50 : 100) : -1

instanceof Operator:

  • $my_array instanceof Array

typeof Operator:

  • typeof $name (where $name="SnapLogic") returns "string"

in Operator:

  • 'name' in $

Spread Operator:

  • Build an array: ["header", ...$body, "footer"]
  • Find maximum value: Math.max(...$numbers)
  • Create object with extra properties: { first_name: $fname, last_name: $lname, ...$extra }