title: How to Append Json object into existing json file in android
body: <p>My json file contains,</p>
  
  <pre><code>{
    "appointments": [
      {
        "appointmentId": "app_001",
        "appointmentTitle": "Appointment Title1",
        "appointmentDate": "2017-11-25",
        "appointmentTime": "10:30",
        "appointmentStatus": "active",
        "appointmentType": "meeting",
        "reminder": {
          "type": "notification",
          "time": "10:15",
          "status": "off"
        },
        "appointmentDescription": "blablablablabla1"
      },
      {
        "appointmentId": "app_002",
        "appointmentTitle": "AppointmentTitle2",
        "appointmentDate": "2017-11-26",
        "appointmentTime": "09:00",
        "appointmentStatus": "done",
        "appointmentType": "exam",
        "reminder": {
          "type": "alarm",
          "time": "08:45",
          "status": "on"
        },
        "appointmentDescription": "blablablablabla2"
      }
    ]
  }
  </code></pre>
  
  <p>I need to put another jsonobject into array, Out put should be like,</p>
  
  <pre><code>{
    "appointments": [
      {
        "appointmentId": "app_001",
        "appointmentTitle": "Appointment Title1",
        "appointmentDate": "2017-11-25",
        "appointmentTime": "10:30",
        "appointmentStatus": "active",
        "appointmentType": "meeting",
        "reminder": {
          "type": "notification",
          "time": "10:15",
          "status": "off"
        },
        "appointmentDescription": "blablablablabla1"
      },
      {
        "appointmentId": "app_002",
        "appointmentTitle": "AppointmentTitle2",
        "appointmentDate": "2017-11-26",
        "appointmentTime": "09:00",
        "appointmentStatus": "done",
        "appointmentType": "exam",
        "reminder": {
          "type": "alarm",
          "time": "08:45",
          "status": "on"
        },
        "appointmentDescription": "blablablablabla2"
      },
      {
        "appointmentId": "app_003",
        "appointmentTitle": "AppointmentTitle3",
        "appointmentDate": "2017-11-26",
        "appointmentTime": "09:00",
        "appointmentStatus": "done",
        "appointmentType": "exam",
        "reminder": {
          "type": "alarm",
          "time": "08:45",
          "status": "on"
        },
        "appointmentDescription": "blablablablabla3"
      }
    ]
  }
  </code></pre>
  
  <p>I used following code segment perform my requirement.</p>
  
  <pre><code>File fileJson = new File(getApplicationContext().getExternalFilesDir("/app"), "app.json");
  
  String strFileJson = getStringFromFile(fileJson.toString());
  
  JSONObject jsonObj = new JSONObject(strFileJson);
  
  jsonObj.put("appointmentId", "app_002");
  jsonObj.put("appointmentTitle", "Appointment Title2");
  jsonObj.put("appointmentDate", "2017-11-21");
  jsonObj.put("appointmentTime", "01:30");
  jsonObj.put("appointmentStatus", "active");
  jsonObj.put("appointmentType", "meeting");
  
  JSONObject reminder = new JSONObject();
  reminder.put("type", "note");
  reminder.put("time", "12:30");
  reminder.put("status", "off");
  jsonObj.put("reminder", reminder);
  jsonObj.put("appointmentDescription", "blablablablabla2");
  
  writeJsonFile(fileJson, jsonObj.toString()); 
  </code></pre>
  
  <p><code>writeJsonFile</code>, <code>getStringFromFile</code>, <code>convertStreamToString</code> functions are,</p>
  
  <pre><code>public static String getStringFromFile(String filePath) throws Exception {
      File fl = new File(filePath);
      FileInputStream fin = new FileInputStream(fl);
      String ret = convertStreamToString(fin);
      //Make sure you close all streams.
      fin.close();
      return ret;
  }
  
  public static String convertStreamToString(InputStream is) throws Exception {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
          sb.append(line).append("\n");
      }
      return sb.toString();
  }
  
  public static void writeJsonFile(File file, String json) {
      BufferedWriter bufferedWriter = null;
      try {
  
          if (!file.exists()) {
              Log.e("App","file not exist");
              file.createNewFile();
          }
  
          FileWriter fileWriter = new FileWriter(file);
          bufferedWriter = new BufferedWriter(fileWriter);
          bufferedWriter.write(json);
  
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              if (bufferedWriter != null) {
                  bufferedWriter.close();
              }
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>but the output I am getting is,</p>
  
  <pre><code>{
    "appointments": [
      {
        "appointmentId": "app_001",
        "appointmentTitle": "Appointment Title1",
        "appointmentDate": "2017-11-25",
        "appointmentTime": "10:30",
        "appointmentStatus": "active",
        "appointmentType": "meeting",
        "reminder": {
          "type": "notification",
          "time": "10:15",
          "status": "off"
        },
        "appointmentDescription": "blablablablabla1"
      },
      {
        "appointmentId": "app_002",
        "appointmentTitle": "AppointmentTitle2",
        "appointmentDate": "2017-11-26",
        "appointmentTime": "09:00",
        "appointmentStatus": "done",
        "appointmentType": "exam",
        "reminder": {
          "type": "alarm",
          "time": "08:45",
          "status": "on"
        },
        "appointmentDescription": "blablablablabla2"
      }
    ],
    "appointmentId": "app_002",
    "appointmentTitle": "Appointment Title2",
    "appointmentDate": "2017-11-21",
    "appointmentTime": "01:30",
    "appointmentStatus": "active",
    "appointmentType": "meeting",
    "reminder": {
      "type": "note",
      "time": "12:30",
      "status": "off"
    },
    "appointmentDescription": "blablablablabla2"
  }
  </code></pre>
  
  <p>Please help me to get required format of json as output. Thanks in advance</p>