{ "operator" : ["values" ... ] }
. For example, { "cat" : ["I love", " ", "pie"] }
results in “I love pie”.
Overview
JSONLogic provides a powerful way to transform and validate product data within calculated attributes. The platform supports both standard JSONLogic operators and custom extensions designed specifically for e-commerce use cases.Data Access Patterns
When writing JSONLogic formulas in Layers, you have access to two types of data:Catalog Attributes
Reference existing catalog attributes using the_attribute:
prefix followed by the attribute code:
Raw Shopify Data
Access raw underlying Shopify data before transformation using the_raw:raw.
prefix:
_raw:raw.variants
- Array of product variants_raw:raw.featured_media
- Featured image data_raw:raw.images
- Array of product images_raw:raw.options
- Product options
Standard JSONLogic Operators
Layers supports all standard JSONLogic operators. For complete documentation of standard operators, see jsonlogic.com/operations.html.Accessing Data
var
Retrieves a value from the data object. This is the most fundamental operator for accessing product attributes.Logic and Boolean Operations
if
Conditional logic with if-then-else structure.and
Logical AND operation. Returns true if all conditions are true.or
Logical OR operation. Returns true if any condition is true.! (not)
Logical NOT operation. Negates a boolean value.!! (double not)
Converts a value to boolean. Returns true if the value exists and is truthy.Comparison Operations
== (equals)
Loose equality comparison.=== (strict equals)
Strict equality comparison (type and value must match).!= (not equals)
Loose inequality comparison.!== (strict not equals)
Strict inequality comparison.> (greater than)
>= (greater than or equal)
< (less than)
<= (less than or equal)
Arithmetic Operations
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo)
Array Operations
map
Transforms each element in an array.filter
Filters an array based on a condition.reduce
Reduces an array to a single value using an accumulator.accumulator
- The accumulated valuecurrent
- The current array element- The third argument is the initial value for the accumulator
all
Returns true if all elements in an array match the condition.some
Returns true if any element in an array matches the condition.none
Returns true if no elements in an array match the condition.in
Checks if a value is in an array.merge
Merges multiple arrays into a single array.String Operations
cat
Concatenates strings.substr
Extracts a substring. Takes the string, start position, and optional length.Miscellaneous Operations
missing
Returns an array of keys that are missing or have falsy values.missing_some
Returns empty array if at least the specified number of keys exist.Custom Layers Operators
Layers extends JSONLogic with custom operators designed for e-commerce use cases.count
Returns the count of elements in an array. Syntax:- Returns
null
if the input isnull
or empty string - Returns the length of the array if valid
- Counting the number of variants in a product
- Calculating the number of images
- Determining the number of options available
parseDate
Parses a date string or timestamp and returns a Unix timestamp (seconds since epoch). Syntax:- String: ISO 8601 date strings, or any format parsable by PHP’s Carbon library
- Numeric: Unix timestamps in seconds or milliseconds (automatically normalized)
null
if the input is invalid
Examples:
Parse an ISO date string:
- Returns
null
if input isnull
or empty string - Automatically converts millisecond timestamps (> 10 digits) to seconds
- Handles both numeric timestamps and string dates
daysSince
Calculates the number of days between a given date and the current date. Syntax:- String: Date strings (ISO 8601 or any format parsable by PHP’s Carbon)
- Numeric: Unix timestamps in seconds or milliseconds
- JSONLogic expression: Output from
parseDate
or other date operations
null
if the input is invalid
Examples:
Calculate days since publication:
- Returns
null
if input isnull
or empty string - Automatically converts millisecond timestamps to seconds
- Returns absolute value (always positive number of days)
- Product age calculations
- Time-based merchandising rules
- Freshness indicators for new products
- Age-based product filtering
now
Returns the current Unix timestamp in seconds. Syntax:- Real-time date comparisons
- Calculating time differences
- Dynamic time-based rules
Complex Examples
Example 1: SKU Coverage Ratio
Calculate the ratio of available variants to total variants:- Maps over variants, converting availability to 1 or 0
- Reduces the array by summing all values
- Divides by the total count of variants
- Result is a ratio between 0 and 1
Example 2: Product Freshness Score
Calculate a freshness score based on when the product was published:Example 3: Has Discounted Variants
Check if any variant has a compare_at_price greater than its price:Example 4: Variant Count by Availability
Count available vs unavailable variants:Best Practices
Use Descriptive Variable Access
Always use the appropriate prefix for clarity:Handle Null Values
Always provide fallback values when data might be missing:Combine Operations for Complex Logic
Build complex calculations by combining multiple operators:Test with Real Data
Use the calculated attribute testing interface in the Layers dashboard to validate your JSONLogic formulas with actual product data before deploying them to production.Keep Formulas Maintainable
For very complex calculations, consider breaking them into multiple calculated attributes that reference each other rather than creating one massive formula.See Also
- Catalog Attributes - Managing searchable, filterable, and sortable attributes
- JSONLogic Official Documentation - Complete reference for standard operators
- Merchandising - Using calculated attributes in merchandising rules
- Sort Orders - Using calculated attributes for custom sorting