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:Example 5: derive a value from tags using mapping rules
Assign a product category based on tag values, evaluating rules top to bottom where the first match wins:- Checks if the product tags contain “outdoor” (case-insensitive) — if so, returns “Outdoor”
- Checks if the product type starts with “winter” (case-insensitive) — if so, returns “Winter”
- Checks if the product type ends with “accessories” (case-insensitive) — if so, returns “Accessories”
- Returns
nullif no rules match
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
- JSONLogic Overview - Overview and data access patterns
- Standard Operators - All standard JSONLogic operators
- Custom Layers Operators - Layers-specific extensions
- Catalog Attributes - Managing searchable, filterable, and sortable attributes
- JSONLogic Official Documentation - Complete reference for standard operators