Liquid Tutorial

JSON & Default Filters

Source: Coding with Robby

default — Fallback Values

The default filter returns a fallback when the left value is nil, false, or empty. Essential for metafields and optional section settings.

Fallbacks for optional content
{{ product.metafields.custom.subtitle | default: product.title }}
{{ section.settings.heading | default: 'Welcome to our store' }}

json — Passing Data to JavaScript

The json filter serializes Liquid objects into valid JSON for embedding in <script> tags. Shopify ensures proper escaping.

Embed full product object for JS
<script type="application/json" id="ProductJson-{{ product.id }}">
  {{ product | json }}
</script>
  1. JavaScript reads the JSON: JSON.parse(document.getElementById('ProductJson-...').textContent)
  2. Use variant data to update price and image on option change
  3. Never use client-side prices for checkout — always server-authoritative
Note: Large | json payloads increase HTML size. Pass only the properties your JavaScript needs: variants, options, media.