title: Slurping JSON collections in Groovy
body: <p>I have a String of JSON that looks like this:</p>
  
  <pre><code>String json = """
      {       
          "content":{
              "response":{
                  "body": [
                      {
                          "firstName":"Jim",
                          "lastName":"Smith"
                      },
                      {
                          "firstName":"Joe",
                          "lastName":"Smith"
                      },
                      {
                          "firstName":"Jane",
                          "lastName":"Smith"
                      }
                  ]
              }
          }
      }
  """
  </code></pre>
  
  <p>I have a POJO that looks like this:</p>
  
  <pre><code>class Person {
      String firstName
      String surname
  }
  </code></pre>
  
  <p>I can change neither the JSON string I am given (it's actually JSON coming back from a web service) nor the POJO (owned/maintained by a different team).</p>
  
  <p><strong>I would like to turn this JSON into a <code>List&lt;Person&gt;</code>.</strong></p>
  
  <p>My attempt via <code>JsonSlurper</code> is failing:</p>
  
  <pre><code>JsonSlurper slurper = new JsonSlurper()
  List&lt;Person&gt; people = []
  // The problem is I don't know how many people there will be so
  // not sure how to index the slurper.
  </code></pre>
  
  <p>I <em>think</em> the best approach is to iterate through the <code>slurper</code> and turn each person JSON object into a <code>Person</code> instance, and then add that person to the list of <code>people</code>. But I'm not that familiar with <code>JsonSlurper</code>'s API or what the best approach even is.</p>