title: How to get Sqlite data as Json using GSON
body: <p>I'm trying to format my <code>sqlite</code> data from all my columns into a <code>JSONArray</code>.</p>
  
  <p>My sqlite data example is as follows:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>id | name | product |
    
   1 | stev |[{"id":"1","title":"box"}] 
  
   1 | Lian |[{"id":"2","title":"bag"}] 
   
   1 | beny |[{"id":"3","title":"pen"}] </code></pre>
  </div>
  </div>
  </p>
  
  <p>I am saving a <code>JSONArray</code> as a string in my database under the column <code>product</code>.</p>
  
  <p>I am trying to create a <code>jsonArray</code> using <code>gson library</code>.</p>
  
  <p>This is what i have tried:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public String composeJSONfromSQLite() {
  
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; offlineList;
          offlineList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
          String selectQuery = "SELECT  * FROM manyofflineCheckouts ";
    
          SQLiteDatabase database = this.getWritableDatabase();
          Cursor cursor = database.rawQuery(selectQuery, null);
    
          if (cursor.moveToFirst()) {
              do {
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                map.put( cursor.getString(0));
                map.put( cursor.getString(1));
                map.put( cursor.getString(2));
                offlineList.add(map);
  
              } while (cursor.moveToNext());
          }
          database.close();
  
          Gson gson = new GsonBuilder().create();
  
          //Use GSON to serialize Array List to JSON
  
          return gson.toJson(offlineList);
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>This however gives me in the format of:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>[
   {
       "id":"1",
       "name":"stev",
       "product":"[{"id":"1","title":"box"}]"
   },
   {
       "id":"2",
       "name":"Lian",
       "product":"[{"id":"2","title":"bag"}]"
   }
  ]</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is not the correct <code>Json</code> format because of the double quotes surrounding product array <code>"product":"[{"id":"1","title":"box"}]"</code></p>
  
  <p>Is there a way to do this?</p>