This tutorial wil teach you how to pretty print a Json Object using javax.json API which is part of Jakarta EE / Java EE API.
By default if you run a toString() of a javax.json.JsonObject a rough output will be displayed with no formatting. By setting the property JsonGenerator.PRETTY_PRINTING into the JsonGeneratorFactory you will have a nicely formatted Json output.
Here is a sample EJB which loads and prints of a JSON file fetched from the classpath:
import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; import javax.json.JsonWriter; import javax.json.JsonWriterFactory; import javax.json.stream.JsonGenerator; import javax.ejb.Stateless; @Stateless public class JsonPrettyPrintService { public String printJSON(String name) throws Exception { JsonReader jsonReader = Json.createReader(this.getClass().getClassLoader().getResourceAsStream(name)); JsonObject obj = jsonReader.readObject(); Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); StringWriter sw = new StringWriter(); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(obj); jsonWriter.close(); return (sw.toString()); } }
Here is our basic example application:
src └── main ├── java │ └── org │ └── jboss │ └── as │ └── quickstarts │ └── helloworld │ ├── HelloWorldServlet.java │ └── JsonPrettyPrintService.java ├── resources │ └── data.json
When calling the front-end Servlet, you will see the nicely formatted JSON text in the Console logs:
15:38:51,596 INFO 15:38:51,596 INFO { 15:38:51,596 INFO "glossary": { 15:38:51,596 INFO "title": "example glossary", 15:38:51,596 INFO "GlossDiv": { 15:38:51,596 INFO "title": "S", 15:38:51,596 INFO "GlossList": { 15:38:51,597 INFO "GlossEntry": { 15:38:51,597 INFO "ID": "SGML", 15:38:51,597 INFO "SortAs": "SGML", 15:38:51,597 INFO "GlossTerm": "Standard Generalized Markup Language", 15:38:51,597 INFO "Acronym": "SGML", 15:38:51,597 INFO "Abbrev": "ISO 8879:1986", 15:38:51,597 INFO "GlossDef": { 15:38:51,597 INFO "para": "A meta-markup language, used to create markup languages such as DocBook.", 15:38:51,597 INFO "GlossSeeAlso": [ 15:38:51,597 INFO "GML", 15:38:51,597 INFO "XML" 15:38:51,598 INFO ] 15:38:51,598 INFO }, 15:38:51,598 INFO "GlossSee": "markup" 15:38:51,598 INFO } 15:38:51,598 INFO } 15:38:51,598 INFO } 15:38:51,598 INFO } 15:38:51,598 INFO }
Source code for the json pretty print demo: https://github.com/fmarchioni/mastertheboss/tree/master/json/prettyprint
JSON Pretty Print using Google API
On the other hand, if you are using Google’s Gson object.
Google’s Gson
is a library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
Google Gson API has an interesting static method “setPrettyPrinting” which is available in the GsonBuilder class. By using this method, you will be able to have out of the box a JSON formatted text:
Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonOutput = gson.toJson(someObject);
Which requires in your pom.xml:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>