Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
In this Page

In this article

Table of Contents
maxLevel2
excludeAdditional Resources|Related Links|Related Information

Expressions can be used in Snaps such as the Mapper (Data) Snap to manipulate data. The expressions are based off a subset of JavaScript and act accordingly unless otherwise noted.

See Using Expressions for specifics on how to enter expressions

Overview

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. Learn more about Using Expressions.

Operators

Comparison OperatorsDescription
Comparison operators
(>, >=, <, <=, ==, !=)
behave the same as Arithmetic operators
Similar to the JavaScript comparison operators except that strict equals (===) and strict not equals (!===) are not supported.
Logical Operators

Arithmetic Operators

(&&, ||, !) Similar to the JavaScript logical operators

String Operators

(+)Similar to the JavaScript string operators except that the shorthand assignment operator (+=) is not supported.


In this section

Child pages (Children Display)



Arithmetic OperatorsDescriptionExample
(+, -, *, /)
operators behave the same as 

Similar to the JavaScript arithmetic operators except that increment (++) and decrement (--) are not supported.

Note

All arithmetic operators return a floating point number. If an integer is desired, then the global function {{parseInt}} should be used to cast the arithmetic expression as an integer.

Example:


1/2 = .5
parseInt(1/2) = 0

Logical Operators

Logical operators (&&, ||, !) behave the same as JavaScript logical operators

String Operators

String operators (+) behave the same as JavaScript string operators except that the shorthand assignment operator (+=) is not supported.

Spread Operators

The spread operator
Spread Operator
(...) This operator makes it easier to build arrays, objects, and call functions where the list of arguments is built at runtime. For instance, while using in an array literal, the value on the right of the operator is inserted into the new array at that position. Similarly, in a function call, the array expands and is used as the arguments to the function. In an object literal, the right side of the operator is another object that has its keys added to the new object.

Examples:

To build a new array that is made up of another array surrounded by two elements:

Code Block
["header", ...$body, "footer"]
If you want to

To find the maximum value in an array of numbers:

Code Block
Math.max(...$numbers)

To create a new object with some fixed values and entries in the "$extra" object:

Code Block
{ first_name: $fname, last_name: $lname, ... $extra }


Special Operators
Panel

Conditional (Ternary)

The conditional operator

(?)

This operator allows you to specify a value to use based on the result of an expression.

 The syntax is as follows:
Code Block
condition ? trueValue : falseValue
The condition expression

 
The condition expression will be evaluated and

the trueValue will

the trueValue will be the result of the expression if the condition evaluates to true,

otherwise the falseValue will

else the falseValue will be the result.

Example

Syntax:

code

condition ? trueValue : falseValue

$text

==

 "NFL"

 ?

 "foo"

 :

Code Block

 "bar"

Example:

$counter

>

1

?

($counter

<

3

?

50

:

100)

:

-1

Panel
instanceof
The instanceof operator returns

The instanceof operator returns True if the given object is an instance of the given type.

The syntax is as follows:code

Syntax:
object

instanceof

type

$my_array instanceof Array

The possible values for the type are: Null, Boolean, String, Number, Object, Array, Date, LocalDate, DateTime, and LocalDateTime.

Example:

Code Block
$my_array instanceof Array 
Panel
match
The match
This operator allows you to conditionally evaluate one of a number of expressions based on whether an input value matches a given pattern.
  See the 
Learn more about this operator, match Control Flow Operator
 section to learn more
.

Example:

paste-code-macro
match
$score
{
90..=100
=>
'A',
80..<90
=>
'B',
70..<80
=>
'C',
_
=>
'F'
}
panel
typeof
The typeof operator

This operator returns the type of a value as a string.

The syntax is as follows: Code Blocktypeof operand

The possible return values are: "boolean", "number", "string", "object", and "array".

panel

Syntax:
typeof operand

typeof $name (Where $name="SnapLogic") returns "String"
in
The in

This operator returns true if the property name or array index is in the given object or array.

The syntax is as follows: Code Block
languagejs

Syntax:

name|index

in

Code Block

operand

Example:

'name'
in
$


Comments

You can add notes to your expressions using comments.

 A

A comment starts with '/*' and ends with '*/', for example:

Info
icon
code
false

/*

say

hello

*/

"Hello,

World!"

The comment

will be

is ignored when evaluating the

expression, it

expression—it is only for the reader's benefit.

Operator Precedence

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

Unsupported Operations

  • 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,

  can be used

.
For a given document data:

code
Info
iconfalse

{

first

    first_name:

 "James",

last


    last_name:

 "Smith"


}

The expression $first_name would return the 'first_name' property which is James.

The

You can also access the 'first_name' property

can also be accessed

by using array notation $['first_name'].
 
JavaScript array accessors can be used also if the object is an array/list.
For a given document data:

[ 1, 2, 3]
 

$[1] would return the value 2.

More complex example


Complex Example:

code
Info
iconfalse

{

names:


    names: ["Joe",

 "Bob",

 "Fred"]


}

$names[2] would return the value Fred.


Arrow Functions

Custom

You can create custom expression language functions

can be created

using the arrow function syntax:

Code Block
// 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

Pipelines.

Example

To multiply all numbers in an array by ten:
[1, 2, 3].map(x => x * 10)

Result:

  [10, 20, 30]
Panel
bgColor#ebf7e1
borderStylesolid

In this Section

Child pages (Children Display)depth2