title: Convert from ResultSet to JSON Object
body: <p>I am trying to convert ResultSet to JSON object. I managed to get a JSON Array object already. Is there a way I can convert JSON object to JSON Array, or from ResultSet to JSON Object directly?</p>
  
  <p>I tried to follow below suggestions in below link.
  <a href="https://stackoverflow.com/questions/7193599/how-can-i-turn-a-jsonarray-into-a-jsonobject/7193753">How can I turn a JSONArray into a JSONObject?</a></p>
  
  <p>But the JSON object I get now is like this:</p>
  
  <pre><code>{"data":[{"patient_key":80},{"eng_surname":"CAN"},{"eng_givename":"NOT"}....]}
  </code></pre>
  
  <p>I wish to have something like this for API calls:</p>
  
  <pre><code>{       "patientKey": 88,
          "hkid": " U0500574",
          "engSurname": "CAN",
          "engGivename": "NOT ", ....}
  </code></pre>
  
  <p>Is there any way I can get my desired result?</p>
  
  <p>This is how I converted to JSON Array.</p>
  
  <pre><code>public static JSONArray convertToJSONArray(ResultSet resultSet)
              throws Exception {
          JSONArray jsonArray = new JSONArray();
          while (resultSet.next()) {
              int total_rows = resultSet.getMetaData().getColumnCount();
              for (int i = 0; i &lt; total_rows; i++) {
                  JSONObject obj = new JSONObject();
                  obj.put(resultSet.getMetaData().getColumnLabel(i + 1)
                          .toLowerCase(), resultSet.getObject(i + 1));
                  jsonArray.put(obj);
              }
          }
          return jsonArray;
      }
  </code></pre>