title: How to Use Freemarker Templates to Output JSON
body: <p>I have an application that needs to process some data and output it as a JSON file. Rather than process everything manually, I thought I could use a templating library to structure the output. Freemarker seemed to offer what I was looking for, but I have run into a problem when dealing with lists.</p>
  <p>The classes for my data model look like this:</p>
  <pre><code>public class Order {    
      public String orderNumber;
      public Date processed;
      public Boolean release; 
      public List&lt;OrderLine&gt; lines;       
  }
  
  public class OrderLine {
      public String unit;
      public Integer quantity;
      public String itemNumber;
  }
  </code></pre>
  <p>The JSON output should look like:</p>
  <pre><code>{&quot;OrderNo&quot;: &quot;1055249&quot;, &quot;OrderDate&quot;: &quot;202103251951&quot;, &quot;Release&quot;: true, &quot;OrderLines&quot;: [
  {&quot;UnitOfMeasure&quot;: &quot;EA&quot;, &quot;Quantity&quot;: &quot;1&quot;, &quot;ItemNo&quot;: &quot;764724473433&quot;},
  {&quot;UnitOfMeasure&quot;: &quot;EA&quot;, &quot;Quantity&quot;: &quot;1&quot;, &quot;ItemNo&quot;: &quot;764724810979&quot;}
  ]}
  </code></pre>
  <p>Notice the list of OrderLines. I can put together a template for the other properties easily enough, but I have no idea how to handle a repeated element like an OrderLine. The built in <strong>list</strong> directive prints out each element but I need a comma separated list of values.</p>
  <p>Does anyone know of a way to do this in freemarker without having to create a custom directive? If not, is there another templating library better suited to what I'm trying to do?</p>