Paris 2024 – JSON Challenge

The Paris 2024 Olympics provided a great opportunity for around 10,500 athletes to compete in 329 events, each event giving an opportunity for competing athletes to win one of the three medals: Gold, Silver and Bronze. All together, 206 countries were represented and the 2024 Olympics also included athletes from the IOC Refugee Team, which regroups displaced and refugee athletes.

For this programming challenge, we are going to search through a list of all the Paris 2024 medallists to answer specific queries and generate some statistics. All information about the Paris 2024 is stored in a JSON file called medals.json.

JavaScript Object Notation (JSON)

JavaScript Object Notation (JSON) is a lightweight, text-based format for storing and exchanging data that is human and machine-readable. It is a standard format that is often used to transfer/retrieve data from a server using an API. It is used in a wide range of web and mobile applications that rely on accessing/exchanging live data.

A JSON file is like a dictionary data structure. It is made up of two primary parts: keys and values. A key/value pair follows a specific syntax:

  • Key: Always a string enclosed in quotation marks
  • Value: Can be a string, number, Boolean expression, array, or object

Complex data structure can be created using JSON by combining {dictionaries} and [arrays].

medals.json

Before attempting this challenge you will need to familiarise yourself with the structure of the provided JSON file: medals.json

Within this file, the main key is “athletes”. It’s value is an array of all the medallists of the Paris 2024 Olympics. Each medallist is stored as a dictionary with 4 keys:

  • name (Lastname firstname): e.g. Marchand Leon
  • gender: e.g. M
  • country: e.g. France
  • medals: An array of all the medals won by this athlete. Each medal within this array is another dictionary consisting of four keys:
    • discipline: e.g. Swimming
    • medal: e.g. Gold
    • event: e.g. Men’s 200m Butterfly
    • date: e.g. 2024-07-31

Here is an extract of the medals.json file showing the data for the first two athletes: Yufei Zhang who won 6 medals for China and Leon Marchand who won 5 medals for France.

{
  "athletes": [
    {
      "name": "Zhang Yufei",
      "country": "China",
      "gender": "F",
      "medals": [
        {
          "discipline": "Swimming",
          "medal": "Bronze",
          "event": "Women's 4 x 100m Medley Relay",
          "date": "2024-08-04"
        },
        {
          "discipline": "Swimming",
          "medal": "Bronze",
          "event": "Women's 4 x 100m Freestyle Relay",
          "date": "2024-07-27"
        },
        {
          "discipline": "Swimming",
          "medal": "Bronze",
          "event": "Women's 50m Freestyle",
          "date": "2024-08-04"
        },
        {
          "discipline": "Swimming",
          "medal": "Silver",
          "event": "Mixed 4 x 100m Medley Relay",
          "date": "2024-08-03"
        },
        {
          "discipline": "Swimming",
          "medal": "Bronze",
          "event": "Women's 100m Butterfly",
          "date": "2024-07-28"
        },
        {
          "discipline": "Swimming",
          "medal": "Bronze",
          "event": "Women's 200m Butterfly",
          "date": "2024-08-01"
        }
      ]
    },
    {
      "name": "Marchand Leon",
      "country": "France",
      "gender": "M",
      "medals": [
        {
          "discipline": "Swimming",
          "medal": "Gold",
          "event": "Men's 200m Breaststroke",
          "date": "2024-07-31"
        },
        {
          "discipline": "Swimming",
          "medal": "Gold",
          "event": "Men's 400m Individual Medley",
          "date": "2024-07-28"
        },
        {
          "discipline": "Swimming",
          "medal": "Gold",
          "event": "Men's 200m Individual Medley",
          "date": "2024-08-02"
        },
        {
          "discipline": "Swimming",
          "medal": "Bronze",
          "event": "Men's 4 x 100m Medley Relay",
          "date": "2024-08-04"
        },
        {
          "discipline": "Swimming",
          "medal": "Gold",
          "event": "Men's 200m Butterfly",
          "date": "2024-07-31"
        }
      ]
    }
  ]
}

Python Code

To be able to read and extract data from our JSON file, using Python we will use the json library. Here is an example of how to use Python code to load the JSON data from the medals.json file. We can then perform a basic linear search to retrieve all the medallists from Great Britain.

import json
 
# load JSON data from file
with open('medals.json','r') as file:
    data = json.load(file)

# Perform a linear search using the JSON data
athletes = data["athletes"]
for athlete in athletes:
    if athlete["country"]=="Great Britain":
       print(athlete["name"])

Let’s use this code to enable the end-user of our program to list all the medallists for their chosen country.

Your Task:

Your task consists of adding extra functions to the above code to perform the following:

     List all the athletes for a given country who won at least one gold medal
     Ask the end-user to enter a discipline. List all the medallists for this discipline.
     Ask the end-user to enter the name of an athlete. The program should list all the medals won by this athlete.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 5

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: