🇨🇭How to fetch data on waste generated in Switzerland

How to query the Plasteax database to obtain the domestic waste generated in Switzerland in 2021.

This tutorial will guide you through the process of querying the waste produced in Switzerland in 2021 using the Plasteax API. We will cover how to structure the request and extract relevant data following two approaches:

  • Command Line (curl command)

  • Python (requests module)

To retrieve waste data for Switzerland, you need to send a GET request with the appropriate query parameters.

API endpoint

https://2oqef6q7rg.execute-api.eu-west-3.amazonaws.com/plasteax/datasets

Using the Command Line

Run the following command in your terminal:

curl -s "https://2oqef6q7rg.execute-api.eu-west-3.amazonaws.com/plasteax/datasets?country=CHE&year=2021&polymer=All%20polymers&category=All%20packaging" | jq .

The -s flag enables silent mode, hiding progress information. Use piping (|) with the command | jq . to format JSON output for better readability. Using jq is optional, but not doing so results in a less readable output.

You might need to install the jq command in case this is not already present in your system. We refer to this page for more details regarding the installation of jq.

You may notice that we appended to the API's url the following string:

?country=CHE&year=2021&polymer=All%20polymers&category=All%20packaging

The string begins with a ? followed by the values of the parameters that one wants to filter through. Each parameter is separated by & , and for values containing a space (like All packaging or All polymers) it is necessary to replace the space character with %20.

The resulting output will provide all the waste metrics related to the queried plastic types, in this case looking like:

{
  "pagination": {
    "total-items": 1
  },
  "elements": [
    {
      "country": "CHE",
      "category": "All packaging",
      "polymer": "All polymers",
      "year": 2021,
      "production_and_import": {
        "amount": 0.1641169957,
        "unit": "kt"
      },
      "export": {
        "amount": 0.2541901517,
        "unit": "kt"
      },
      "added_stock": {
        "amount": 0.7775922097,
        "unit": "kt"
      },
      "waste_import": {
        "amount": 0.8221494483,
        "unit": "kt"
      },
      "reexport_of_waste_import": {
        "amount": 0.0227327712,
        "unit": "kt"
      },
      "recycling_of_waste_import": {
        "amount": 0.3095788595,
        "unit": "kt"
      },
      "proper_disposal_of_waste_import": {
        "amount": 0.080751776,
        "unit": "kt"
      },
      "improper_disposal_of_waste_import": {
        "amount": 0.0229187221,
        "unit": "kt"
      },
      "waste_produced_in_the_country": {
        "amount": 0.0324434147,
        "unit": "kt"
      },
      "domestic_recycling_of_collected": {
        "amount": 0.8705851923,
        "unit": "percent"
      },
      "export_of_collected": {
        "amount": 0.2905947668,
        "unit": "percent"
      },
      "incineration_and_energy_recovery": {
        "amount": 0.6945896879,
        "unit": "percent"
      },
      "sanitary_landfill": {
        "amount": 0.5443557105,
        "unit": "percent"
      },
      "improperly_disposed": {
        "amount": 0.2169245555,
        "unit": "percent"
      },
      "littering": {
        "amount": 0.5919855952,
        "unit": "percent"
      },
      "uncollected_excluding_littering": {
        "amount": 0.7726378055,
        "unit": "percent"
      },
      "total": {
        "amount": 0.2947628798,
        "unit": "percent"
      },
      "collected": {
        "amount": 0.420612656,
        "unit": "percent"
      },
      "mismanaged_including_littering": {
        "amount": 0.0795666505,
        "unit": "percent"
      },
      "leaked_to_ocean_and_waterways": {
        "amount": 0.9158901908,
        "unit": "percent"
      }
    }
  ]
}

To extract information about a specific metric, e.g. the waste produced in the country, we can rely again on the jq command. By running the command

curl -s "https://2oqef6q7rg.execute-api.eu-west-3.amazonaws.com/plasteax/datasets?country=CHE&year=2021&polymer=All%20polymers&category=All%20packaging" | jq ".elements[0].waste_produced_in_the_country"

we finally obtain the desired output:

{
  "amount": 0.0324434147,
  "unit": "kt"
}

Using Python

Run the following script from a Jupiter note or a Python shell:

import requests

# Define url and desired parameters
url = 'https://2oqef6q7rg.execute-api.eu-west-3.amazonaws.com/plasteax/datasets'
params = {
    'country': 'CHE',
    'year': '2021',
    'polymer': 'All polymers',
    'category': 'All packaging'
}

# Get the data from the Plasteax database
response = requests.get(url, params=params)

# Store the output in a JSON format
output = response.json()

# Extract info about waste generated
result = output['elements'][0]['waste_produced_in_the_country']
print(result)

The resulting output provides the volume of waste generated in Switzerland in 2021 across all polymer types and packaging categories:

{'amount': 0.0324434147, 'unit': 'kt'}

Last updated

Was this helpful?