JSON with array and non-array data with Groovy JsonBuilder
<p>I need to create a JSON string in my Groovy script which has some elements that are array and some which are not. For example the below..</p>

<pre><code> {
 "fleet": {
   "admiral":"Preston",
   "cruisers":  [
      {"shipName":"Enterprise"},
      {"shipName":"Reliant"}
   ]
  }
}
</code></pre>

<p>I found <a href="https://stackoverflow.com/questions/13973342/how-to-use-groovy-builder-to-generate-an-array-type-json">this post</a> but the answers either didn't make sense or didn't apply to my example.</p>

<p>I tried the below in code...</p>

<pre><code> def json = new groovy.json.JsonBuilder()
 def fleetStr = json.fleet {
         "admiral" "Preston"
         cruisers {
            {shipName: "[Enterprise]"},  {shipName: "[Reliant]"}
       }
   }
</code></pre>

<p>But it gives an exception...</p>

<pre><code> Ambiguous expression could be either a parameterless closure expression or an isolated open code block
</code></pre>