title: Removing a json object from a json file in Java
body: <p>I have this json file I downloaded online:</p>
  
  <pre><code> {
  "price": 1,
  "empty": [
    0,
    0,
    0,
    0,
    0
  ],
  "lowValue": 0,
  "highValue": 0
  },
  </code></pre>
  
  <p>and I want to delete everything from </p>
  
  <blockquote>
    <p>"empty": [</p>
  </blockquote>
  
  <p>to</p>
  
  <blockquote>
    <p>],</p>
  </blockquote>
  
  <p>I've spent a few hours looking at regex stuff and I can't seem to figure out how to make it do what I want it to do.</p>
  
  <p><strong>Edit:</strong>
  Annamalai Thangaraj's method works until I add more to the file.</p>
  
  <pre><code>{
  "price": 1,
  "empty": [
    0,
    0,
    0,
    0,
    0
  ],
  "lowValue": 0,
  "highValue": 0
  },
  {
  "price": 500,
  "empty": [
    5,
    0,
    3,
    6,
    9
  ],
  "lowValue": 4,
  "highValue": 2
  }
  </code></pre>
  
  <p>which now I'm given an error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException: com.google.gson.JsonArray cannot be cast to com.google.gson.JsonObject</p>
  </blockquote>
  
  <p>My code is exactly:</p>
  
  <pre><code>public static void go() throws IOException {
      JsonObject jsonObject = (JsonObject)JsonParser.parse(new FileReader(location));
      jsonObject.remove("empty");
  
      JsonArray jsonArray = (JsonArray)JsonParser.parse(new FileReader(location));
  
  
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
      JsonParser jp = new JsonParser();
      JsonElement je = jp.parse(jsonObject.toString());
      String prettyJsonString = gson.toJson(je);
  
      FileWriter file = new FileWriter(System.getProperties().getProperty("user.home")+"\\output.json");
      try {
          file.write(prettyJsonString);
          System.out.println("Successfully wrote JSON object to file.");
  
      } catch (IOException e) {
          e.printStackTrace();
  
      } finally {
          file.flush();
          file.close();
      }
  }
  </code></pre>