title: Append text field data to an existing JSON file in java
body: <p>I have a text field where user can enter data, once data is received i want to append it to existing JSON file.</p>
  
  <p>I am able to read the existing data and getting the text field value, but while appending the new data with existing data I'm facing problem.</p>
  
  <p>Error:</p>
  
  <pre><code>com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 18 path $
  </code></pre>
  
  <p>Below code :</p>
  
  <p>JSON File :</p>
  
  <pre><code>{"vins":[{"vin":"544554"},{"vin":"54554"}]}
  </code></pre>
  
  <p>Text field value : String test ="3689";
  so it should be appended as :</p>
  
  <pre><code>{"vins":[{"vin":"544554"},{"vin":"54554"},{"vin":"3689"}]}
  </code></pre>
  
  <p>Filereadwrite class :</p>
  
  <pre><code>public class JSONFIlewrite {
      public static String Vinno_Read;
      public static List&lt;String&gt; linststring;
      public static void main(String[] args) {
          try {
  
              JSONParser parser = new JSONParser();
              Object obj = parser.parse(new 
              FileReader("C:\\Amaresh\\Test\\sample_json.json"));
  
              JSONObject jsonObject = (JSONObject) obj;
              System.out.println(jsonObject);
              linststring= new ArrayList();
              // loop array
              JSONArray msg = (JSONArray) jsonObject.get("vins");
              Iterator iterator = msg.iterator();
  
              while (iterator.hasNext()) {
                  Vinno_Read = iterator.next().toString();
  
                  linststring.add(Vinno_Read);
  
  
              }
  
              String list_string = "";
              System.out.println(linststring);
  
              for(String temp:linststring){
                  list_string += temp;
                  System.out.println("amar1"+list_string);
              }
  
              System.out.println("amar"+list_string);
              Vin vin4 = new Vin();
              vin4.setVin("76354273462");
              Vins vins = new Vins();
              vins.addVins(vin4);
  
              Gson gson = new Gson();
              //String jsonValue=list_string;
  
              String jsonValue = gson.toJson(list_string).toString();
              System.out.println("json--"+jsonValue);
              Vins vins1 = gson.fromJson(jsonValue, Vins.class);
              System.out.println("ddd"+vins1);
              Vin vin = new Vin();
              vin.setVin("544554");
  
              vins1.addVins(vin);
  
              jsonValue = gson.toJson(vins1).toString();
  
              BufferedWriter writer = null;
              try {
                  writer = new BufferedWriter(new FileWriter("C:\\Amaresh\\Test\\sample_json.json"));
                  writer.write(jsonValue);
                  System.out.println("Test"+jsonValue);
  
              } catch (IOException e) {
              } finally {
                try {
                  if (writer != null)
                      writer.close();
                } catch (IOException e) {
              }
          }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Setter Getter classes :</p>
  
  <pre><code>public class Vin {
      @Expose
      private String vin;
  
      public String getVin() {
          return vin;
      }
  
  public void setVin(String vin) {
      this.vin = vin;
  }
  
  }
  
  
  public class Vins {
      @Expose
      List&lt;Vin&gt; vins = new ArrayList&lt;&gt;();
  
      public List&lt;Vin&gt; getVins() {
          return vins;
      }
  
      public void addVins(Vin vin) {
          this.vins.add(vin);
      }
  }
  </code></pre>