Chapter 2: Retrieving Match Stats.
We will jump straight into retrieving match stats from a fixture of choice in this exercise.
However, if you would like to learn about how we adopted ‘filters’ and ‘includes’ to narrow down to specific stats of interest, you can read all about this in our previous blog with Julia.
2.2 Retrieving Stats using Includes.
Using an include statement with ‘statistics.type,’ we will retrieve all the match stats from the fixture ID 19134600, which was the game between Manchester City and Tottenham.
That fixtured ended in a 4-0 loss for the Sky Blues. It was Pep Guardiola’s record fifth successive defeat of his managerial career.
Using our URL of interest, we will write a piece of code in Groovy to retrieve the stats.
https://api.sportmonks.com/v3/football/fixtures/FixtureID?api_token=API_Token&include=statistics.type
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import java.net.HttpURLConnection
import java.net.URL
def fetchData(apiUrl) {
try {
// Create a connection to the API URL
def url = new URL(apiUrl)
HttpURLConnection connection = (HttpURLConnection) url.openConnection()
connection.setRequestMethod("GET")
connection.setConnectTimeout(5000)
connection.setReadTimeout(5000)
// Read the response
def response = connection.inputStream.text
// Parse the JSON response
def json = new JsonSlurper().parseText(response)
// Pretty print the JSON
def prettyJson = JsonOutput.prettyPrint(JsonOutput.toJson(json))
println("Data:\n${prettyJson}")
return json
} catch (Exception e) {
println("Error: ${e.message}")
}
return null
}
// Provide the API URL
def apiUrl = "https://api.sportmonks.com/v3/football/fixtures/19134600?api_token=API_Token&include=statistics.type"
// Fetch and pretty print the data
fetchData(apiUrl)
Data:
{
"data": {
"id": 19134600,
"sport_id": 1,
"league_id": 8,
"season_id": 23614,
"stage_id": 77471288,
"group_id": null,
"aggregate_id": null,
"round_id": 339246,
"state_id": 5,
"venue_id": 151,
"name": "Manchester City vs Tottenham Hotspur",
"starting_at": "2024-11-23 17:30:00",
"result_info": "Tottenham Hotspur won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732383000,
"statistics": [
{
"id": 154543270,
"fixture_id": 19134600,
"type_id": 56,
"participant_id": 6,
"data": {
"value": 9
},
"location": "away",
"type": {
"id": 56,
"name": "Fouls",
"code": "fouls",
"developer_name": "FOULS",
"model_type": "statistic",
"stat_group": "defensive"
}
},
{
"id": 154461825,
"fixture_id": 19134600,
"type_id": 42,
"participant_id": 9,
"data": {
"value": 23
},
"location": "home",
"type": {
"id": 42,
"name": "Shots Total",
"code": "shots-total",
"developer_name": "SHOTS_TOTAL",
"model_type": "statistic",
"stat_group": "offensive"
}
},
{
"id": 154543030,
"fixture_id": 19134600,
"type_id": 80,
"participant_id": 9,
"data": {
"value": 566
},
"location": "home",
"type": {
"id": 80,
"name": "Passes",
"code": "passes",
"developer_name": "PASSES",
"model_type": "statistic",
"stat_group": "overall"
}
},
{
"id": 154541889,
"fixture_id": 19134600,
"type_id": 86,
"participant_id": 6,
"data": {
"value": 7
},
"location": "away",
"type": {
"id": 86,
"name": "Shots On Target",
"code": "shots-on-target",
"developer_name": "SHOTS_ON_TARGET",
"model_type": "statistic",
"stat_group": "offensive"
}
},
{
"id": 154541880,
"fixture_id": 19134600,
"type_id": 86,
"participant_id": 9,
"data": {
"value": 5
},
"location": "home",
"type": {
"id": 86,
"name": "Shots On Target",
"code": "shots-on-target",
"developer_name": "SHOTS_ON_TARGET",
"model_type": "statistic",
"stat_group": "offensive"
}
},
{
"id": 154543137,
"fixture_id": 19134600,
"type_id": 56,
"participant_id": 9,
"data": {
"value": 19
},
"location": "home",
"type": {
"id": 56,
"name": "Fouls",
"code": "fouls",
"developer_name": "FOULS",
"model_type": "statistic",
"stat_group": "defensive"
}
},
{
"id": 154511651,
"fixture_id": 19134600,
"type_id": 45,
"participant_id": 9,
"data": {
"value": 58
},
"location": "home",
"type": {
"id": 45,
"name": "Ball Possession %",
"code": "ball-possession",
"developer_name": "BALL_POSSESSION",
"model_type": "statistic",
"stat_group": "overall"
}
},
{
"id": 154511652,
"fixture_id": 19134600,
"type_id": 45,
"participant_id": 6,
"data": {
"value": 42
},
"location": "away",
"type": {
"id": 45,
"name": "Ball Possession %",
"code": "ball-possession",
"developer_name": "BALL_POSSESSION",
"model_type": "statistic",
"stat_group": "overall"
}
},
{
"id": 154461826,
"fixture_id": 19134600,
"type_id": 42,
"participant_id": 6,
"data": {
"value": 9
},
"location": "away",
"type": {
"id": 42,
"name": "Shots Total",
"code": "shots-total",
"developer_name": "SHOTS_TOTAL",
"model_type": "statistic",
"stat_group": "offensive"
}
},
{
"id": 154543328,
"fixture_id": 19134600,
"type_id": 80,
"participant_id": 6,
"data": {
"value": 427
},
"location": "away",
"type": {
"id": 80,
"name": "Passes",
"code": "passes",
"developer_name": "PASSES",
"model_type": "statistic",
"stat_group": "overall"
}
}
]
},
"subscription": [
{
"meta": {
"trial_ends_at": "2024-10-16 14:50:45",
"ends_at": null,
"current_timestamp": 1734483733
},
"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": 66,
"remaining": 2986,
"requested_entity": "Fixture"
},
"timezone": "UTC"
}
Explanation
HttpURLConnection is the core Java class used for HTTP requests, which supports GET, POST, PUT, DELETE, and more HTTP methods.
JsonSlurper parses the JSON response into a Groovy object (Map/List), while JsonOutput.prettyPrint converts the parsed JSON object back to a well-formatted string to enable a clear and readable format.