1.3 Formatting the JSON Response
Going back to the piece of code in section 1.2, we will modify the code to do some formatting and have the JSON response printed in a much more readable way.
Before doing so, we will have to add an external library called Gson. Depending on your version of the IDE, you will have to navigate to the add library view, just as seen in the screen shot below.
Once the Gson library has been imported into the project, then you can include the import statements as seen below.
You can then copy and paste this piece of code.
import java.net.HttpURLConnection
import java.net.URL
import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
fun main() {
val url = "https://api.sportmonks.com/v3/football/leagues?api_token=API_token" // Replace with your API token
val urlObj = URL(url)
val connection = urlObj.openConnection() as HttpURLConnection
try {
connection.requestMethod = "GET"
val responseCode = connection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
// Read the response
val response = connection.inputStream.bufferedReader().use { it.readText() }
// Parse and pretty-print the JSON response
val jsonElement = JsonParser.parseString(response)
val gson = GsonBuilder().setPrettyPrinting().create() // Enable pretty printing
val prettyJson = gson.toJson(jsonElement)
println(prettyJson) // Print formatted JSON
} else {
println("GET request failed with response code: $responseCode")
}
} catch (e: Exception) {
println("An error occurred: ${e.message}")
} finally {
connection.disconnect()
}
}
{
"data": [
{
"id": 2,
"sport_id": 1,
"country_id": 41,
"name": "Champions League",
"active": true,
"short_code": "UEFA CL",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/2.png",
"type": "league",
"sub_type": "cup_international",
"last_played_at": "2024-10-23 19:00:00",
"category": 1,
"has_jerseys": false
},
{
"id": 5,
"sport_id": 1,
"country_id": 41,
"name": "Europa League",
"active": true,
"short_code": "UEFA EL",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/5/5.png",
"type": "league",
"sub_type": "cup_international",
"last_played_at": "2024-10-24 19:00:00",
"category": 1,
"has_jerseys": false
},
{
"id": 8,
"sport_id": 1,
"country_id": 462,
"name": "Premier League",
"active": true,
"short_code": "UK PL",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/8/8.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-21 19:00:00",
"category": 1,
"has_jerseys": false
},
{
"id": 9,
"sport_id": 1,
"country_id": 462,
"name": "Championship",
"active": true,
"short_code": "UK Champ",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/9/9.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-23 19:00:00",
"category": 2,
"has_jerseys": false
},
{
"id": 12,
"sport_id": 1,
"country_id": 462,
"name": "League One",
"active": true,
"short_code": "UK L1",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/12/12.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-22 18:45:00",
"category": 2,
"has_jerseys": false
},
{
"id": 14,
"sport_id": 1,
"country_id": 462,
"name": "League Two",
"active": true,
"short_code": "UK L2",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/14/14.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-22 18:45:00",
"category": 2,
"has_jerseys": false
},
{
"id": 17,
"sport_id": 1,
"country_id": 462,
"name": "National League",
"active": true,
"short_code": "UK NL",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/17/17.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-23 18:45:00",
"category": 3,
"has_jerseys": false
},
{
"id": 20,
"sport_id": 1,
"country_id": 462,
"name": "Vanarama National League North",
"active": true,
"short_code": "UK NLN",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/20/20.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-23 18:45:00",
"category": 4,
"has_jerseys": false
},
{
"id": 23,
"sport_id": 1,
"country_id": 462,
"name": "Community Shield",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/23/23.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-08-10 14:00:00",
"category": 2,
"has_jerseys": false
},
{
"id": 24,
"sport_id": 1,
"country_id": 462,
"name": "FA Cup",
"active": true,
"short_code": "UK FA Cup",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/24/24.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-10-15 18:45:00",
"category": 2,
"has_jerseys": false
},
{
"id": 27,
"sport_id": 1,
"country_id": 462,
"name": "Carabao Cup",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/27/27.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-10-01 18:45:00",
"category": 2,
"has_jerseys": false
},
{
"id": 30,
"sport_id": 1,
"country_id": 462,
"name": "Fa Trophy",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/30/30.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-10-05 14:00:00",
"category": 2,
"has_jerseys": false
},
{
"id": 32,
"sport_id": 1,
"country_id": 462,
"name": "Premier League Cup",
"active": true,
"short_code": "ENG DL2",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/0/32.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-10-22 18:00:00",
"category": 3,
"has_jerseys": false
},
{
"id": 35,
"sport_id": 1,
"country_id": 462,
"name": "Premier League U21",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/3/35.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2016-05-12 10:30:00",
"category": 3,
"has_jerseys": false
},
{
"id": 38,
"sport_id": 1,
"country_id": 462,
"name": "Premier League International Cup",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/6/38.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-10-22 18:30:00",
"category": 3,
"has_jerseys": false
},
{
"id": 39,
"sport_id": 1,
"country_id": 462,
"name": "EFL Trophy",
"active": true,
"short_code": "UK EFLT",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/7/39.png",
"type": "league",
"sub_type": "domestic_cup",
"last_played_at": "2024-10-08 18:45:00",
"category": 2,
"has_jerseys": false
},
{
"id": 42,
"sport_id": 1,
"country_id": 462,
"name": "Premier League U18",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/10/42.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-19 11:00:00",
"category": 3,
"has_jerseys": false
},
{
"id": 44,
"sport_id": 1,
"country_id": 462,
"name": "Wsl 2 Women",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/12/44.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-20 13:00:00",
"category": 3,
"has_jerseys": false
},
{
"id": 45,
"sport_id": 1,
"country_id": 462,
"name": "Women's Super League",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/13/45.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-20 17:45:00",
"category": 3,
"has_jerseys": false
},
{
"id": 48,
"sport_id": 1,
"country_id": 462,
"name": "Professional Development League",
"active": true,
"short_code": "UK U23 DL",
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/16/48.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-24 18:00:00",
"category": 4,
"has_jerseys": false
},
{
"id": 51,
"sport_id": 1,
"country_id": 462,
"name": "Non League Premier: Isthmian",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/19/51.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-22 18:45:00",
"category": 4,
"has_jerseys": false
},
{
"id": 53,
"sport_id": 1,
"country_id": 462,
"name": "Non League Div One: Isthmian North",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/21/53.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-23 18:45:00",
"category": 4,
"has_jerseys": false
},
{
"id": 54,
"sport_id": 1,
"country_id": 462,
"name": "Non League Div One: Southern Central",
"active": true,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/22/54.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2024-10-22 18:45:00",
"category": 4,
"has_jerseys": false
},
{
"id": 57,
"sport_id": 1,
"country_id": 462,
"name": "Non League Div One: Northern North",
"active": false,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/25/57.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2020-12-26 21:00:12",
"category": 4,
"has_jerseys": false
},
{
"id": 60,
"sport_id": 1,
"country_id": 462,
"name": "Non League Div One: Isthmian South",
"active": false,
"short_code": null,
"image_path": "https://cdn.sportmonks.com/images/soccer/leagues/28/60.png",
"type": "league",
"sub_type": "domestic",
"last_played_at": "2020-11-04 09:00:27",
"category": 4,
"has_jerseys": false
}
],
"pagination": {
"count": 25,
"per_page": 25,
"current_page": 1,
"next_page": "https://api.sportmonks.com/v3/football/leagues?page=2",
"has_more": true
},
"subscription": [
{
"meta": {
"trial_ends_at": "2024-10-16 14:50:45",
"ends_at": null,
"current_timestamp": 1729811556
},
"plans": [
{
"plan": "Enterprise plan (loyal)",
"sport": "Football",
"category": "Advanced"
},
{
"plan": "Enterprise Plan",
"sport": "Cricket",
"category": "Standard"
},
{
"plan": "Formula One",
"sport": "Formula One",
"category": "Standard"
}
],
"add_ons": [
{
"add_on": "All-in News API",
"sport": "Football",
"category": "News"
},
{
"add_on": "pressure index add-on",
"sport": "Football",
"category": "Default"
},
{
"add_on": "Enterprise Plan Predictions",
"sport": "Football",
"category": "Predictions"
},
{
"add_on": "xG Advanced",
"sport": "Football",
"category": "Expected"
}
],
"widgets": [
{
"widget": "Sportmonks Widgets",
"sport": "Football"
}
]
}
],
"rate_limit": {
"resets_in_seconds": 3079,
"remaining": 2998,
"requested_entity": "League"
},
"timezone": "UTC"
}
Here’s the explanation. We used GsonBuilder().setPrettyPrinting().create() to create a Gson instance that formats the JSON response from the url with proper indentation.
The GsonBuilder() is used to configure Gson to pretty-print the output, while the toJson() method converts the parsed jsonElement into a well-formatted, human-readable string.