title: Grails: Easy and efficient way to parse JSON from a Request
body: <p>Please pardon me if this is a repeat question. I have been through some of the questions/answers with a similar requirement but somehow got a bit overwhelmed and confused at the same time. My requirement is:</p>
  
  <ul>
  <li>I get a JSON string/object as a request parameter. ( eg: params.timesheetJSON )</li>
  <li>I then have to parse/iterate through it.</li>
  </ul>
  
  <p>Here is the JSON that my grails controller will be receiving:</p>
  
  <pre><code>{
      "loginName":"user1",
      "timesheetList":
      [
          {
              "periodBegin":"2014/10/12",
              "periodEnd":"2014/10/18",
              "timesheetRows":[
                  {
                      "task":"Cleaning",
                      "description":"cleaning description",
                      "paycode":"payCode1"
                  },
                  {
                      "task":"painting",
                      "activityDescription":"painting description",
                      "paycode":"payCode2"
                  }
              ]
          }
      ],
      "overallStatus":"SUCCESS"
  }
  </code></pre>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>How can I retrieve the whole JSON string from the request? Does request.JSON be fine here? If so, will request.JSON.timesheetJSON yield me the actual JSON that I want as a JSONObject?</p></li>
  <li><p>What is the best way to parse through the JSON object that I got from the request? Is it grails.converters.JSON? Or is there any other easy way of parsing through? Like some API which will return the JSON as a collection of objects by automatically taking care of parsing. Or is programatically parsing through the JSON object the only way?</p></li>
  </ol>
  
  <p>Like I said, please pardon me if the question is sounding vague. Any good references JSON parsing with grails might also be helpful here.</p>
  
  <p><em>Edit</em>: There's a change in the way I get the JSON string now. I get the JSON string as a request paramter.</p>
  
  <pre><code>String saveJSON // This holds the above JSON string.
  
  def jsonObject = grails.converters.JSON.parse(saveJSON) // No problem here. Returns a JSONObject. I checked the class type.
  def jsonArray = jsonArray.timesheetList // No problem here. Returns a JSONArray. I checked the class type.
  println "*** Size of jsonArray1: " + jsonArray1.size() // Returns size 1. It seemed fine as the above JSON string had only one timesheet in timesheetList
  
  def object1 = jsonArray[1] // This throws the JSONException, JSONArray[1] not found. I tried jsonArray.getJSONObject(1) and that throws the same exception.
  </code></pre>
  
  <p>Basically, I am looking to seamlessly iterate through the JSON string now.</p>