Faraj Farook Faraj Farook - 1 year ago 1575
JSON Question

Thymeleaf print JSON string as JSON object into a javascript variable

In Specific

I need a way to print

representation of a string value into the html page via thymeleaf.

In detail

I'm having a
model attribute
which contains a string which is actually a string representation of the


<script th:inline="javascript">
var value = [[${data.scriptValue}]];

print the variable as below

var value = '[[\"asd\",\"3\"],[\"asd\",\"1\"],[\"asdasd\",\"1\"]]';

But I want something like this as a

var value = [["asd","3"],["asd","1"],["asdasd","1"]];

How to do this in thymeleaf?

Note: I know I can do this from
but i need a way to do this from thymeleaf :)

Answer Source

Update - 2015/12/24

This feature is available in Thymeleaf 3

Refer The Thymeleaf textual syntax in https://github.com/thymeleaf/thymeleaf/issues/395

// Unescaped (actual answer)
var value = [(${data.scriptValue})];
var value = [# th:utext="${data.scriptValue}"/];

// Escaped
var value = [[${data.scriptValue}]];
var value = [# th:text="${data.scriptValue}"/];

It's not possible at Thymeleaf 2. As Patric LC mentioned, there are two issues reported for this.

  1. unescaped inline for scripts/css #12

  2. Use Jackson for Javascript inlining of JSON #81