Solved

How can I escape quotes in Liquid for correct Schema.org output?

pramodraam
Shopify Partner
77 2 9

 

            {
                "@type": "ListItem",
                "position": 2,
                "item": {
                  "@id": "{{ routes.collections_url }}/{{ collection.handle }}",
                  "name": "{{collection.title}}"
                }
            },

"name": "{{collection.title}}" in the code above is supposed to output value like "48 to 59". But because of quotes that are not getting escaped, the code is throwing an invalid Schema.org error. The output below is from a collection page on the site https://www.perfectmakeupmirrors.com/collections/led-mirrors-48-59

 

The erroneous JSON code from this page is below:

 

{"@context": "https://schema.org","@type": "BreadcrumbList","itemListElement": [{"@type": "ListItem","position": 1,"item": {"@id": "/","name": "Home"}},{"@type": "ListItem","position": 2,"item": {"@id": "/collections/led-mirrors-48-59","name": "48" to 59""}}]}

Could you please help with the code to escape quotes?

 

 

 

Accepted Solution (1)
pramodraam
Shopify Partner
77 2 9

This is an accepted solution.

Thank you very much, Paul. But the solutions didn't work. I'm more interested in something like "{{collection.title | json }}

View solution in original post

Replies 3 (3)

PaulNewton
Shopify Partner
6339 580 1350

https://stackoverflow.com/questions/15637429/how-to-escape-double-quotes-in-json 

To then do so with liquid,

Either use the JSON filter escape it 

{{collection.title | json }}

https://shopify.dev/docs/api/liquid/filters/json 

 

Or remove or replace the quotes, code size and formatted to have example coloring to highlight easy to misread characters ( in purple and orange).

{% liquid
double_quote = '"'
escaped_double_quote = '\"'
%}
{{collection.title | remove: double_quote }}
{{collection.title | replace: double_quote, escaped_double_quote }}

 

💣double_quote and escaped_double_quote  variable have TWO single-quote marks with characters between them between them and NO spaces in the value. When working with quote characters it can be much saner to work with preset variables than risk making a hard to find/read typo, i.e. ''"'"'".

https://shopify.dev/docs/api/liquid/filters/remove 

https://shopify.dev/docs/api/liquid/filters/replace 

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


pramodraam
Shopify Partner
77 2 9

This is an accepted solution.

Thank you very much, Paul. But the solutions didn't work. I'm more interested in something like "{{collection.title | json }}

pramodraam
Shopify Partner
77 2 9