Expression Language Examples
This reference provides practical examples of using the expression language in common scenarios. These examples demonstrate real-world use cases for data transformation, validation, filtering, and formatting in SnapLogic pipelines.
Object Examples
Conditional Expression (If-Then-Else):
| Expression | $.hasOwnProperty('query.fred') ? $query.fred : 'not present in input' |
| Description | This example demonstrates an if-then-else expression pattern. |
| Use Case |
To set a default pipeline variable in a document object, use this expression for a mapper variable
|
String Examples
Checking for a Non-empty String:
| Expression | $customer.lastname.trim() || 'default' |
| Description |
The expression verifies if |
Checking for an Existing Value:
| Expression | $.hasOwnProperty('customer.lastname') || 'default' |
| Description |
The expression verifies if |
Checking for a Non-empty String AND an Existing Value:
| Expression | ($.hasOwnProperty('customer.lastname') && $customer.lastname.trim()) || 'default' |
| Description |
The expression verifies if a property exists in the input document and if |
Creating an Email Address from First Initial and Last Name:
| Expression |
OR
|
| Description |
Either expression grabs the first letter of the |
| Example Result |
Where Result: "[email protected]" |
Date Examples
Creating and Formatting a Date in a Specific Timezone:
| Expression | Date.now().toLocaleDateString('{\"timeZone\":\"PST\", \"format\":\"yyyy-MM-dd\"}') |
| Description |
The expression creates the current date time in the PST timezone and formats its output into a string in the form of yyyy-MM-dd. |
Formatting a Date to Include Letters:
| Expression | Date.now().toLocaleDateTimeString('{\"format\":\"yyyy-MM-dd\'T\'hh:mm:ss.SSS\'Z\'\"}') |
| Description |
The expression creates the current date time and formats its output into a string to include the letter "T" and "Z". Characters outside of normal SQLDate specials can be added by escaping them. |
| Example Result | 2017-03-15T10:30:45.123Z |
Creating an ISO-formatted Date in the Current Timezone:
| Expression | Date.now().toLocaleDateString('{\"timeZone\":\"PST\"}') |
| Description |
The expression creates the current date time in the local time zone and formats its output into a string for the ISO date time format. |
Parsing a Date:
| Expression | Date.parse($StandardDate) |
| Description |
This expression parses a string representation of
|
Parsing a Non-standard Date:
| Expression |
OR
|
| Description |
These examples parse the date into a DateTime object using the provided format. |
Formatting Today's Date to Display as the Current Month:
| Expression | Date.now().toLocaleDateString({"format":"MMMM"}) |
| Description |
This expression takes today's date and formats the output in a string indicating the month in text. |
| Example Result | "March" |
Filtering Examples
Filtering for Two Possible Values:
| Expression | $Priority == "Resolve Immediately" || $Priority == "High Attention" |
| Description |
Used in a Filter Snap, this expression finds only those items that have |
Filtering by Multiple Fields:
| Expression | $Workflow == "Ready For Testing" && (($Priority == "Resolve Immediately") || ($Priority == "High Attention")) |
| Description |
Used in a Filter Snap, this expression finds only those items that are in the |
Filtering by Date within Timeframe:
| Expression | $ClosedDate >= Date.now().minusHours(24) |
| Description |
Used in a Filter Snap, this expression finds only those items that have a |
Other Transformation Examples
Making a Field Value a Link:
| Expression | '<a href="' + $Link + '">' + $ID + '</a>' |
| Description |
This expression turns the value of the |
| Example Result |
Where Result: <a href="http://example.com">12345</a> |
Mapping Values to an Array:
This series of expressions maps data from:
[
{
"ORG_ASSIGNMENT": [
{
"ORGTXT": "Network Services",
"JOBTXT": "Service technician",
"POSTXT": "Manager"
}
]
}
]
To name/value pairs within an array:
[
"Organization": {
"Unit": [
[
"Position",
"Manager"
],
[
"Grade",
"Service technician"
],
[
"Division",
"Network Services"
]
]
}
]
| Expression 1 | ['Position', jsonPath($, "ORG_ASSIGNMENT[*].POSTXT").toString()] |
| Target Path 1 | $Users.User[*].Organization.Unit[0] |
| Expression 2 | ['Grade', jsonPath($, "ORG_ASSIGNMENT[*].JOBTXT").toString()] |
| Target Path 2 | $Users.User[*].Organization.Unit[1] |
| Expression 3 | ['Division', jsonPath($, "ORG_ASSIGNMENT[*].ORGTXT").toString()] |
| Target Path 3 | $Users.User[*].Organization.Unit[2] |
Complex Scenarios
Nested Conditional with Multiple Checks:
// Set priority based on multiple conditions
$status == "Critical" ? "P1" :
$status == "High" && $age > 24 ? "P2" :
$status == "Medium" ? "P3" :
"P4"
Combining Date and String Operations:
// Generate timestamped filename
$filename.replace(".csv", "") + "_" +
Date.now().toLocaleDateString({"format":"yyyyMMdd_HHmmss"}) +
".csv"
Data Validation Pattern:
// Validate email format and set to null if invalid
$email.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/) ? $email : null
Dynamic Field Selection:
// Select field based on environment parameter
_environment == "prod" ? $production_field : $test_field
Safe Navigation with Multiple Levels:
// Safely access nested properties with defaults
$.hasPath("customer.address.city") ?
$customer.address.city :
"Unknown"
Array Transformation:
// Transform array of objects to array of specific values
$items.map(item => item.name + " (" + item.id + ")")
Error Rate Calculation:
// Calculate error percentage
{
"total": snap.in.totalCount,
"errors": snap.error.totalCount,
"errorRate": ((snap.error.totalCount / snap.in.totalCount) * 100).toFixed(2) + "%"
}
Best Practices for Expression Examples
- Null Safety: Always check for existence before accessing nested properties using hasOwnProperty() or hasPath().
- Default Values: Use the OR operator (||) or ternary operator (? :) to provide fallback values.
- Readability: Break complex expressions into multiple steps or use intermediate variables when possible.
- Performance: Avoid unnecessary function calls in loops or repetitive operations.
- Type Consistency: Ensure data types match when performing comparisons or operations.
- Date Formatting: Always specify the date format explicitly when parsing or formatting dates.
- String Escaping: Properly escape special characters in regular expressions and HTML.
- Testing: Test expressions with edge cases including null, empty strings, and missing fields.