title: How to convert List of JSON Objects to JSON Array using Java EE?
body: <p>I am trying to convert some JSON objects to a JSON Array. I am fetching the data from a mysql database. My current code is using this loop:</p>
  
  <p>Main Class:</p>
  
  <pre><code>    public class Main 
      {
          ResultSet resultSet=null;
          TestDAO testDAO = new TestDAO();
  
          public List&lt;JSONObject&gt; getJsonObject()
          {
              resultSet = testDAO.getResultSet();
              List&lt;JSONObject&gt; resList = JsonServices.getFormattedResult(resultSet);
              return resList;
          }
  
          public JSONArray getJsonArray() throws Exception {
              resultSet = testDAO.getResultSet();
              JSONArray jsonArray = new JSONArray();
              while (resultSet.next()) {
                  int total_rows = resultSet.getMetaData().getColumnCount();
                  JSONObject obj = new JSONObject();
                  for (int i = 0; i &lt; total_rows; i++) {
                      obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
                  }
                jsonArray.put(obj);
              }
              return jsonArray;
          }
  
  
          public static void main(String args[]) throws Exception
          {
              Main m = new Main();
              List&lt;JSONObject&gt; jObj = m.getJsonObject();
              JSONArray jArr = m.getJsonArray();
  
              for(int i=0;i&lt;jArr.length();i++)
              {
                  System.out.println(jArr.get(i));
              }
  
  
  
          /*for(int i=0;i&lt;jObj.size();i++)
          {
              System.out.println(jObj.get(i));
          }*/
      }
  }
  </code></pre>
  
  <p>Service class:</p>
  
  <pre><code>public class JsonServices 
  {
      public static List&lt;JSONObject&gt; getFormattedResult(ResultSet rs)
      {
          List&lt;JSONObject&gt; resList = new ArrayList&lt;JSONObject&gt;();
          try
          {
              ResultSetMetaData rsMeta = rs.getMetaData();
              int columnCnt = rsMeta.getColumnCount();
              List&lt;String&gt; columnNames = new ArrayList&lt;String&gt;();
  
              for(int i=1;i&lt;=columnCnt;i++)
              {
                  columnNames.add(rsMeta.getColumnName(i).toUpperCase());
              }
              while(rs.next())
              {
                  JSONObject obj = new JSONObject();
                  for (int i=1;i&lt;=columnCnt;i++)
                  {
                      String key = columnNames.get(i-1);
                      String value = rs.getString(i);
                      obj.put(key, value);
                  }
                  resList.add(obj);
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
          finally
          {
              try
              {
                  rs.close();             
              }
              catch (SQLException e)
              {
                  e.printStackTrace();
              }
          }
          return resList;
      }
  
  }
  </code></pre>
  
  <p>My current loop returns data in this format:</p>
  
  <pre><code>{"fineloadinmetric":"648265.32","waterlevel":"45.59","fdate":"2017-08-29","discharge":"43391.657","runinthousand":"156.771","courseloadinmetric":"50538.462","mediumloadinmetric":"176808.134","floodflag":"false"}
  {"fineloadinmetric":"856793.621","waterlevel":"44.94","fdate":"2017-08-28","discharge":"46843.674","runinthousand":"212.579","courseloadinmetric":"43957.152","mediumloadinmetric":"132528.51","floodflag":"false"}
  {"fineloadinmetric":"898789.111","waterlevel":"45.66","fdate":"2017-08-27","discharge":"43436.507","runinthousand":"159.727","courseloadinmetric":"53053.74","mediumloadinmetric":"169307.45","floodflag":"false"}
  </code></pre>
  
  <p>I need the JSON array like:</p>
  
  <pre><code>[{
          name: 'fineloadinmetric',
          data: [648265.32, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
      }, {
          name: 'waterlevel',
          data: [45.59, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
      }, {
          name: 'fdate',
          data: [2017-08-29, 2017-08-28, 2017-08-27, 2017-08-26, 2017-08-25, 2017-08-24, 2017-08-23, 2017-08-22]
      }, {
          name: 'discharge',
          data: [43391.657, null, 7988, 12169, 15112, 22452, 34400, 34227]
      }, {
          name: 'runinthousand',
          data: [156.771, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
      }, {
          name: 'courseloadinmetric',
          data: [50538.462, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
      }, {
          name: 'mediumloadinmetric',
          data: [176808.134, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
      }, {
          name: 'floodflag',
          data: [false, true, false, true, false, false, false, true]
      }]
  </code></pre>
  
  <p>Please ignore the data, it's from a different sample.
  I will be using this JSON Array to geenrate some charts using Highcharts.js.</p>