Looping through a list of Java objects in JavaScript with Thymeleaf
<p>In a Spring Boot project, I have a list of users, in Java <code>List&lt;User&gt;</code><br>
I pass it from the Controller to the template, I am able to loop through this list using a HTML list <code>ul</code> but I am not able to do it in JavaScript:</p>

<pre><code>&lt;script layout:fragment="script" th:inline="javascript"&gt;
/*&lt;![CDATA[*/
var users = /*[[${users}]]*/ [];
for (var i = 0; i &lt; users.length; i++) {
    console.log(i); // Obviously here I would like to access the User properties
}
/*]]&gt;*/
&lt;/script&gt;
</code></pre>

<p>I get this error:</p>

<pre><code>Error during execution of processor
'org.thymeleaf.standard.processor.text.StandardTextInliningTextProcessor'  
</code></pre>

<p>How do we loop through lists and access Java object properties in JavaScript with Thymeleaf?<br>
Thanks.</p>

<p><strong>EDIT: what I have discovered so far</strong></p>

<p>My User class is a JPA entity with a Country property (Country is another JPA entity):</p>

<pre><code>@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id")
private Country country;
</code></pre>

<ul>
<li>If I pass a list of users <strong>without the country</strong> fetched the JavaScript works.</li>
<li>If I pass a list of countries and loop through (to test the Country class by itself), it works too.</li>
<li>If I pass a list of users where I set the country myself (not fetched from database) with the same values as the database contains, it works</li>
<li>If I retrieve the country from the database, then set it to the user, it fails.</li>
</ul>

<p>So it seems like the problem is because the Country entity is created/mapped to the User a certain way by Spring Data/Hibernate that Thymeleaf can not deserialize it...</p>