title: Java Json pretty print javax.json
body: <p>I'm trying to pretty print json with the javax.json API</p>
  
  <p>The code I'm currently using is as follows:</p>
  
  <pre><code>private String prettyPrint(String json) {
          StringWriter sw = new StringWriter();
  
          try {
              JsonReader jr = Json.createReader(new StringReader(json));
  
              JsonObject jobj = jr.readObject();
  
  
  
              Map&lt;String, Object&gt; properties = new HashMap&lt;&gt;(1);
              properties.put(JsonGenerator.PRETTY_PRINTING, true);
  
  
              JsonGeneratorFactory jf = Json.createGeneratorFactory(properties);
              JsonGenerator jg = jf.createGenerator(sw);
  
              jg.write(jobj).close();
  
  
          } catch (Exception e) {
          }
  
          String prettyPrinted = sw.toString();
  
          return prettyPrinted;
      }
  </code></pre>
  
  <p>I'm getting the following exception:</p>
  
  <pre><code>11:47:08,830 ERROR [stderr] (EJB default - 1) javax.json.stream.JsonGenerationException: write(JsonValue) can only be called in array context
  11:47:08,835 ERROR [stderr] (EJB default - 1)   at org.glassfish.json.JsonGeneratorImpl.write(JsonGeneratorImpl.java:301)
  11:47:08,838 ERROR [stderr] (EJB default - 1)   at org.glassfish.json.JsonPrettyGeneratorImpl.write(JsonPrettyGeneratorImpl.java:55)
  11:47:08,841 ERROR [stderr] (EJB default - 1)   at org.proactive.rest.VideoFeedService.prettyPrint(VideoFeedService.java:247)
  11:47:08,843 ERROR [stderr] (EJB default - 1)   at org.proactive.rest.VideoFeedService.requestVideoFeedData(VideoFeedService.java:124)
  11:47:08,845 ERROR [stderr] (EJB default - 1)   at org.proactive.rest.VideoFeedService.run(VideoFeedService.java:86)
  11:47:08,848 ERROR [stderr] (EJB default - 1)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  11:47:08,850 ERROR [stderr] (EJB default - 1)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  11:47:08,852 ERROR [stderr] (EJB default - 1)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  11:47:08,854 ERROR [stderr] (EJB default - 1)   at java.lang.reflect.Method.invoke(Method.java:483)
  </code></pre>