0

I have a project with Java 8, bringing data to the frontend with Thymeleaf. The data entity I want to ouput in my frontend is calles logs and is a list of entities that have an attribute containig json data. I output it in the html file as follows:

<script type="text/javascript">
    let logs = [[${logs}]];
</script>

The json has the following structure:

{
  "oldest": 712367620,
  "youngest": 712378606,
  "measurements": []
}

But on the page it appears with the double quotes escaped which completely messes up the js:

<script type="text/javascript">
    let logs = [LogEntity(id=52, success=true, runtimes={
        &quot;oldest&quot; : 712367620,
        &quot;youngest&quot; : 712378606,
        &quot;measurements&quot; : []
    })]
</script>

How can I prevent the escaping of the double quotes in the json string? I work with php and twig in another project and there it is as simple as {{ logs|raw }}. Is there some kind of modifier in thymeleaf like |raw in php twig?

Security is not an issue since I have complete control over the data objects.

1 Answer 1

0

You have to enable JavaScript inlining with th:inline="javascript":

<script type="text/javascript" th:inline="javascript">
    let logs = [[${logs}]];
</script>

(I'm not sure what you mean by "json data". Do you mean you have a String that contains JSON?)

Sign up to request clarification or add additional context in comments.

3 Comments

Exactly. The attribute runtimes is a string that contains json
Your code gives me <script type="text/javascript"> let logs = "{\r\n \"oldest\" : 752646765,\r\n \"youngest\" : 752747578,\r\n \"measurements\" : []\r\n}"; </script> which I would need without the backslashes before the double quotes and without the \r\n
I found the solution here: stackoverflow.com/questions/29733594/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.