Description
While customizing Cornerstone we used the JSONparse handlebar helper, with little documentation on it and no examples to work with.
With some trial/error we found that it will parse a JSON string and use the result as the context.
Example of how we're using it when looping through custom fields on a product page:
{{#startsWith '{' value}} {{#JSONparse value}} {{#if img}} {{> components/page/hero-banner backgroundImage=(cdn (concat 'livecdn:/product_images/uploaded_images/' img)) }} {{else}} {{> components/page/hero-banner}} {{/if}} {{/JSONparse}} {{else}} {{> components/page/hero-banner backgroundImage=(cdn (concat 'livecdn:/product_images/uploaded_images/' value)) }} {{/startsWith}}
In short, we check to see if the custom field value starts with {, if so then we assume it is valid JSON and parse it for use with the hero-banner component.
We found that if the JSON string we attempt to parse is not valid then this will completely break the page with a server error. Definitely not an ideal solution for what we're doing.
What is the proper usage for JSONparse?
Did we discover something that is unintended?
Is there a way we can attempt to parse the JSON and fall back to default content if it's invalid (without generating a server error)?
Thank you in advance!
Previously posted here: bigcommerce/cornerstone#1485
Related Issue?: #31