Pretty-Print a JSON in Java
This post will discuss how to pretty-print a JSON in Java.
1. Using GSON Library
To enable pretty-print with Gson, you can configure the Gson instance to output Json for pretty printing. The idea is to use GsonBuilder to construct a Gson instance and then invoke its setPrettyPrinting() configuration method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.List; class Company { public String name; public int employees; public List<String> offices; public Company(String name, int employees, List<String> offices) { this.name = name; this.employees = employees; this.offices = offices; } } public class Main { public static void main(String[] args) { String jsonString = "{\"name\":\"Google\",\"employees\":140000," + "\"offices\":[\"Mountain View\",\"Los Angeles\",\"New York\"]}"; Gson gson = new GsonBuilder().setPrettyPrinting().create(); Company company = gson.fromJson(jsonString, Company.class); String prettyJsonString = gson.toJson(company); System.out.println(prettyJsonString); } } |
Output:
{
"name": "Google",
"employees": 140000,
"offices": [
"Mountain View",
"Los Angeles",
"New York"
]
}
Alternatively, you can use JsonParser to parse Json into a parse tree of Json elements. The parse() or parseString() method parses the specified JSON string into a parse tree, which then can be passed to the toJson() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; public class Main { public static void main(String[] args) { String jsonString = "{\"name\":\"Google\",\"employees\":140000," + "\"offices\":[\"Mountain View\",\"Los Angeles\",\"New York\"]}"; JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(jsonString); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String prettyJsonString = gson.toJson(jsonElement); System.out.println(prettyJsonString); } } |
Output:
{
"name": "Google",
"employees": 140000,
"offices": [
"Mountain View",
"Los Angeles",
"New York"
]
}
2. Using JSON-Java Library
Alternatively, if you prefer JSON-Java Library, you can use the JSONObject.toString(indentFactor) method to get a pretty-printed JSON representation of the JSONObject. A typical invocation for this method would look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.json.JSONException; import org.json.JSONObject; public class Main { public static void main(String[] args) { String jsonString = "{\"name\":\"Google\",\"employees\":140000," + "\"offices\":[\"Mountain View\",\"Los Angeles\",\"New York\"]}"; try { JSONObject json = new JSONObject(jsonString); String prettyJsonString = json.toString(4); System.out.println(prettyJsonString); } catch (JSONException e) { e.printStackTrace(); } } } |
Output:
{
"employees": 140000,
"name": "Google",
"offices": [
"Mountain View",
"Los Angeles",
"New York"
]
}
3. Using Jackson Library
The Jackson library (com.fasterxml.jackson.databind) also provides the factory method writerWithDefaultPrettyPrinter() to serialize objects with the pretty printer for indentation. Here’s how the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) { String jsonString = "{\"name\":\"Google\",\"employees\":140000," + "\"offices\":[\"Mountain View\",\"Los Angeles\",\"New York\"]}"; ObjectMapper mapper = new ObjectMapper(); String prettyJsonString = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(jsonString); System.out.println(prettyJsonString); } } |
Output:
{
"employees": 140000,
"name": "Google",
"offices": [
"Mountain View",
"Los Angeles",
"New York"
]
}
That’s all about pretty-printing a JSON in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)