title: How to generate JSON custom data-attribute using thymeleaf?
body: <p>I have a requirement to embed JSON as custom data-attribute using thymeleaf. I have hacky working solution like this.</p>
  
  <pre><code>&lt;th:block th:fragment="myFragment(val)"&gt;
      &lt;div th:attr="data-json='{&amp;quot;x&amp;quot;:&amp;quot;'+ ${val}+'-x&amp;quot;}'"&gt;&lt;/div&gt;
  &lt;/th:block&gt;
  </code></pre>
  
  <p>Result HTML</p>
  
  <pre><code>&lt;div data-json="{"x": "abc-x"}"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Embedding JSON template with <code>&amp;quot;</code> is not a good solution. </p>
  
  <p>Is there a way to generate JSON with a cleaner fragment in thymeleaf? If so, can someone share such a fix?</p>
  
  <p>FYI, I have <code>@Data (lombok)</code> and <code>@JsonInclude (jackson library)</code> at disposal in Java model but I don't want to update my model as the thymeleaf fragment is independent of the model (depends only on val).</p>