title: Java: Generating JSON: How to get rid of - Generating incomplete JSON
body: <p>In the below Java code I am trying to generate JSON which should look like this:
  On running the program I am getting the following exception:<br>
  <strong>Update: I just solved my problem and the below exception went away: The updated code is below</strong><br>
  <br> And line 50: corresponds to: <strong>generator.close()</strong> in the Java code listed below</p>
  
  <pre><code>Exception in thread "main" javax.json.stream.JsonGenerationException: Generating incomplete JSON
      at org.glassfish.json.JsonGeneratorImpl.close(JsonGeneratorImpl.java:509)
      at com.myhome.myapp.GenerateJson.main(GenerateJson.java:50)
  </code></pre>
  
  <p><br></p>
  
  <p>Could anyone point out where I am messing up the code and how I can correct it? Thanks a lot.</p>
  
  <hr>
  
  <pre><code>{
          "X-Awesome-Results": [
              {
                  "results": {
                      "subjectLine": "Hello, Hello",
                      "Rule": "Block Sadness",
                      "match": "true",
                      "content": {
                          "scanResult": "Detected unhappiness that needs to be cleaned out "
                      }
                  }
              }
          ]
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import javax.json.Json;
  import javax.json.stream.JsonGenerator;
   import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
   import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  import com.google.gson.JsonObject;
  import com.google.gson.JsonParser;
  /**
   Jars used are:
      1) gson-2.3.1.jar
       2) jackson-annotations-2.5.1.jar
        3) jackson-core-2.5.1.jar
        4)jackson-databind-2.5.1.jar
        5)javax.json-1.0.4.jar
        6)javax.json-api-1.0.jar
        7)json-simple-1.1.1.jar
  */
  
  
  public class GenerateJson {
  
      public static void main(String[] args) {
  
          FileWriter writer = null;
          JSONParser parser = new JSONParser();
          Object simpleObj = null;
  
          try {
                  //File to write the generated json into
              writer = new FileWriter("C:/path/to/storing/generated/json/jsonFormatted.json");
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          // JsonGenerator to create JSONObject and store it to file location mentioned above
          JsonGenerator generator = Json.createGenerator(writer);
  
          generator.writeStartObject().writeStartArray("X-Awesome-Results")
      .writeStartObject()
           .writeStartObject("results")
              //.writeStartObject()
                  .write("subjectLine", "Hello, Hello")
                  .write("policyEntryOrRule", "Block Sadness")
                  .write("match", "true")
          .writeStartObject("content")
              .write("scanResult", "Detected sadness. Need more optimism")
              .writeEnd()
          .writeEnd() 
      .writeEnd()
      .writeEnd()
      .writeEnd();
  
          generator.close();
  
  try {
              simpleObj = parser.parse(new FileReader("C:/path/to/storing/generated/json/jsonFormatted.json"));
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (ParseException e) {
              e.printStackTrace();
          }
          System.out.println("Simple JSON Result:\n" + simpleObj.toString());
  
              String prettyFinalJson = myPrettyJSONUtility(finalJson);
              System.out.println("\nPretty JSON Result:\n" + prettyFinalJson);
  
          }
  
          public static String myPrettyJSONUtility(String simpleJSON) {
              JsonParser avidParser = new JsonParser();
              JsonObject json = avidParser.parse(simpleJSON).getAsJsonObject();
  
              Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
              String prettyJson = prettyGson.toJson(json);
  
              return prettyJson;
          }
  
  
      }
  </code></pre>