Search code examples
thymeleafjson-ld

Specify Double Quotes for Thymeleaf's inline Javascript


I'm trying to add some Schema Markup to a site built with Thymeleaf. My first thought was to use the ld+json method:

<script type="application/ld+json" th:inline="javascript">
{
    "@context": "http://schema.org",
    "@type": "LocalBusiness",
    "address": {
      "@type": "PostalAddress",
      "streetAddress": /*[[ ${C:Location.street}]]*/,
      "addressLocality": /*[[ ${C:Location.city}]]*/,
      "addressRegion": /*[[ ${C:Location.state}]]*/,
      "postalCode": /*[[ ${C:Location.zipcode}]]*/
    },
}
</script>

But Thymeleaf outputs those strings in single quotes, which apparently doesn't validate as correct JSON when checking with https://developers.google.com/structured-data/testing-tool/

Is it possible to tell Thymeleaf to use double quotes here? I can do the HTML microdata markup if all else fails but I'd prefer not to since it's not as pretty and modular.


Solution

  • I tried to use text mode:

    <script type="application/ld+json" th:inline="text">
        {
            "@context": "http://schema.org",
            "@type": "EmailMessage",
            "potentialAction": {
                "@type": "ViewAction",
                "url": "[[ @{${url}} ]]",
                "name": "[[ #{message.button.text} ]]"
            }
        }
    </script>
    

    Output:

    <script type=3D"application/ld+json" xml:space=3D"preserve">
        {
            "@context": "http://schema.org",
            "@type": "EmailMessage",
            "potentialAction": {
                "@type": "ViewAction",
                "url": "https://watch-movies.com/watch",
                "name": "Watch movie"
            }
        }
    </script>