Strange parsing error when applying json liquid filter to the product object

Topic summary

Issue: Converting a Shopify product object to JSON via the Liquid “| json” filter and embedding it in a fieldset’s data attribute results in the value being truncated at the first space, causing JSON.parse to fail (missing closing brace). This setup follows Shopify’s subscription selling plan docs, with JavaScript reading dataset.product and parsing it.

Details: The template aims to expose the product object to JS by attaching the JSON to a custom data attribute. Console logs show the JSON string cuts off at the first space; changing the product title to remove spaces only shifts the truncation to the next space. An attached image of the logs is central to understanding the failure.

Technical context: Liquid “| json” serializes objects to JSON; HTML data attributes are read via element.dataset; JSON.parse expects a complete JSON string.

Current status: Multiple participants report the same parsing error and ask if a solution was found. No fixes, decisions, or action items are provided. The discussion remains unresolved and open, with key questions about why the data attribute value is being truncated.

Summarized with AI on January 28. AI used: gpt-5.

I am working on my first liquid block in a theme app extension. The end goal is to build a product form that handles subscription selection, as per Shopify’s docs: https://shopify.dev/themes/pricing-payments/subscriptions/selling-plans

The idea is that the liquid template exposes the product object to related JavaScript as JSON. The product is turned into JSON using the “| json” liquid filter, then attached to a custom data attribute on a element.

{% form 'product', product %}

{% endform %}

This is the exact code from the docs page linked above.

Then, the related JavaScript file queries for the fieldset element, accesses the product object and parses it using JSON.parse() to be used in dynamic operations.

var sellingPlanContainer = document.querySelector('.selling-plan-fieldset')
console.log('product object as unparsed JSON: ', sellingPlanContainer.dataset.product)

var product = JSON.parse(sellingPlanContainer.dataset.product)
console.log('product object parsed into Javascript: ', product)

This is also exact code from the docs, minus the logs.

Then something really strange and frustrating happens, as you can see from the logs. The JSON output from the liquid code to the data attribute stops at the first space character. This obviously throws an error when the browser attempts to parse it because there’s no closing curly brace on the object string. As you can see, I even changed the title of the product to a string without any spaces just to see what would happen, but the error happened at the next space.

Any ideas on why this could be happening/possible solutions? Thanks in advance!

2 Likes

Did you ever get this solved? Running into the same error. Thank you!

2 Likes

I’m also getting the same error while parsing.

1 Like

Hi, Did you get any solution to this?

1 Like