Skip to main content

Complex examples

Example 1: SKU coverage ratio

Calculate the ratio of available variants to total variants:
{
  "/": [
    {
      "reduce": [
        {
          "map": [
            { "var": "_raw:raw.variants" },
            { "if": [ { "var": "available" }, 1, 0 ] }
          ]
        },
        { "+": [ { "var": "accumulator" }, { "var": "current" } ] },
        0
      ]
    },
    { "count": { "var": "_raw:raw.variants" } }
  ]
}
Explanation:
  1. Maps over variants, converting availability to 1 or 0
  2. Reduces the array by summing all values
  3. Divides by the total count of variants
  4. Result is a ratio between 0 and 1

Example 2: product freshness score

Calculate a freshness score based on when the product was published:
{
  "if": [
    {
      "<": [
        {
          "daysSince": {
            "parseDate": { "var": "_attribute:published_at" }
          }
        },
        7
      ]
    },
    "new",
    {
      "if": [
        {
          "<": [
            {
              "daysSince": {
                "parseDate": { "var": "_attribute:published_at" }
              }
            },
            30
          ]
        },
        "recent",
        "established"
      ]
    }
  ]
}

Example 3: has discounted variants

Check if any variant has a compare_at_price greater than its price:
{
  "some": [
    { "var": "_raw:raw.variants" },
    {
      "and": [
        { "!!": { "var": "compare_at_price" } },
        {
          ">": [
            { "var": "compare_at_price" },
            { "var": "price" }
          ]
        }
      ]
    }
  ]
}

Example 4: variant count by availability

Count available vs unavailable variants:
{
  "reduce": [
    {
      "map": [
        { "var": "_raw:raw.variants" },
        {
          "if": [
            { "var": "available" },
            { "eachKey": { "available": 1, "unavailable": 0 } },
            { "eachKey": { "available": 0, "unavailable": 1 } }
          ]
        }
      ]
    },
    {
      "eachKey": {
        "available": {
          "+": [
            { "var": ["accumulator.available", 0] },
            { "var": "current.available" }
          ]
        },
        "unavailable": {
          "+": [
            { "var": ["accumulator.unavailable", 0] },
            { "var": "current.unavailable" }
          ]
        }
      }
    },
    { "eachKey": { "available": 0, "unavailable": 0 } }
  ]
}

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:
{
  "if": [
    {
      "in": [
        "outdoor",
        { "lower": { "var": "_attribute:tags" } }
      ]
    },
    "Outdoor",
    {
      "if": [
        {
          "startsWith": [
            { "lower": { "var": "_attribute:product_type" } },
            "winter"
          ]
        },
        "Winter",
        {
          "if": [
            {
              "endsWith": [
                { "lower": { "var": "_attribute:product_type" } },
                "accessories"
              ]
            },
            "Accessories",
            null
          ]
        }
      ]
    }
  ]
}
Explanation:
  1. Checks if the product tags contain “outdoor” (case-insensitive) — if so, returns “Outdoor”
  2. Checks if the product type starts with “winter” (case-insensitive) — if so, returns “Winter”
  3. Checks if the product type ends with “accessories” (case-insensitive) — if so, returns “Accessories”
  4. Returns null if no rules match

Best practices

Use descriptive variable access

Always use the appropriate prefix for clarity:
// Good
{ "var": "_attribute:price" }
{ "var": "_raw:raw.variants" }

// Avoid ambiguity
{ "var": "price" }

Handle null values

Always provide fallback values when data might be missing:
{
  "or": [
    { "var": ["_attribute:published_at", null] },
    { "var": ["_attribute:created_at", null] }
  ]
}

Combine operations for complex logic

Build complex calculations by combining multiple operators:
{
  "daysSince": {
    "parseDate": {
      "or": [
        { "var": ["_attribute:published_at", null] },
        { "var": ["_attribute:created_at", null] }
      ]
    }
  }
}

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