title: How to get a specific value from a JSON string
body: <p>I have a JSON string like below:</p>
  
  <pre><code>{
   "VCAP_SERVICES": {
      "amazon-s3": [
         {
           "credentials": {
                "accesskey": "somevalue",
                "bucketname": "somevalue1"
            }
            "name": "foo"
         },
         {
           "credentials": {
                "accesskey": "someothervalue",
                "bucketname": "someothervalue1"
            }
            "name": "bar"
          }
      ]
    }
  }
  </code></pre>
  
  <p>I'm parsing it as below:</p>
  
  <pre><code>import org.codehaus.groovy.grails.web.json.JSONObject
  import grails.converters.JSON
  JSONObject myJson = JSON.parse(myString)
  </code></pre>
  
  <p>I would like to place values for <code>accesskey</code> and <code>bucketname</code> for both <code>foo</code> and <code>bar</code> in different variables</p>
  
  <pre><code>def foo_accesskey = null
  def foo_bucketname = null
  def bar_accesskey = null
  def bar_bucketname = null
  </code></pre>
  
  <p>I tried the following which doesn't seem to be working:</p>
  
  <pre><code>myJson["amazon-s3"].each {id, data -&gt; 
      if (id == "foo") {
          foo_accesskey = myJson["amazon-s3"]["credentials"]["accesskey"]
          foo_bucketname = myJson["amazon-s3"]["credentials"]["bucketname"]
      }
      else if (id == "bar") {
          bar_accesskey = myJson["amazon-s3"]["credentials"]["accesskey"]
          bar_bucketname = myJson["amazon-s3"]["credentials"]["bucketname"]
      }
  }
  </code></pre>