Pretty Printing JSON data in Kotlin
This post will discuss how to pretty print Json data in Kotlin.
1. Using GSON Library
To pretty-print a Json output with the Gson library, you can configure it for pretty-printing using the setPrettyPrinting()
function. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.gson.GsonBuilder class Company( var name: String, var employees: Int, var offices: List<String> ) fun main() { val jsonString = "{\"name\":\"Microsoft\",\"employees\":180000," + "\"offices\":[\"Washington\",\"Virginia\",\"India\"]}" val gson = GsonBuilder().setPrettyPrinting().create() val company = gson.fromJson(jsonString, Company::class.java) val prettyJsonString = gson.toJson(company) println(prettyJsonString) } |
Output:
{
"name": "Google",
"employees": 180000,
"offices": [
"Washington",
"Virginia",
"India"
]
}
The fromJson
function deserializes the specified Json into an object of the specified class. If the class information is not available, you can use JsonParser
to parse Json into a parse tree of Json elements with either the parse()
or parseString()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.gson.GsonBuilder import com.google.gson.JsonParser fun main() { val jsonString = "{\"name\":\"Google\",\"employees\":180000," + "\"offices\":[\"Washington\",\"Virginia\",\"India\"]}" val jsonParser = JsonParser() val jsonElement = jsonParser.parse(jsonString) val gson = GsonBuilder().setPrettyPrinting().create() val prettyJsonString = gson.toJson(jsonElement) println(prettyJsonString) } |
Output:
{
"name": "Google",
"employees": 180000,
"offices": [
"Washington",
"Virginia",
"India"
]
}
2. Using Jackson Library
If you prefer the Jackson library, use the writerWithDefaultPrettyPrinter()
function to output Json using the pretty printer for indentation. Here’s how the code would look like:
1 2 3 4 5 6 7 8 9 10 11 |
import com.fasterxml.jackson.databind.ObjectMapper fun main() { val jsonString = "{\"name\":\"Google\",\"employees\":180000," + "\"offices\":[\"Washington\",\"Virginia\",\"India\"]}" val mapper = ObjectMapper() val prettyJsonString = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(jsonString) println(prettyJsonString) } |
Output:
{
"employees": 180000,
"name": "Google",
"offices": [
"Washington",
"Virginia",
"India"
]
}
3. Using Json for Java Library
The Json for Java library provides the JsonObject.toString()
function to output a pretty-printed representation of the Json object. It can be used as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.json.JsonException import org.json.JsonObject fun main() { val jsonString = "{\"name\":\"Google\",\"employees\":180000," + "\"offices\":[\"Washington\",\"Virginia\",\"India\"]}" try { val json = JsonObject(jsonString) val prettyJsonString = json.toString(4) println(prettyJsonString) } catch (e: JsonException) { e.printStackTrace() } } |
Output:
{
"employees": 180000,
"name": "Google",
"offices": [
"Washington",
"Virginia",
"India"
]
}
That’s all about pretty printing Json data in Kotlin.
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 :)