Home > database >  Getting a JSONDecode error and I'm not completely understanding as to why it's coming up
Getting a JSONDecode error and I'm not completely understanding as to why it's coming up

Time:08-15

I'm working with this API and I'm pretty new to both APIs and python, but I keep getting a requests.exceptions.JSONDecodeError Expecting value: line 1 column 1 (char 0) when I try to run this code.

This is my code so far:

api_base = "https://ergast.com/api/f1/"

@app.route('/', methods=["GET", "POST"])
def home_route():

return render_template('index.html')

@app.route('/season', methods=["GET", "POST"])
def show_season():

raceYear = request.form.get("season_year")

resp = requests.get(f"{api_base}", params={
    "Race season": raceYear})

results = (resp.json())

return jsonify(results)

Now, I've also tried editing the jsonify portion as well, thinking it might be the problem and I changed it to:

results = resp.json()

return jsonify(results)

But I get the same error message.

Essentially, I'm trying to test the API and make sure that it works and for what I want to do, but also just to see if I understand the process. The end goal is to have the ability to use the API and search with it to find the results I want from a form that I'd submit. In this case, I'm looking for the schedule of the races for a season. So I have the "raceYear" set up on an HTML form where I'd enter "2021" and I would find the results of the schedule for that season.

Any insight or guidance in the right direction would be appreciated!

CodePudding user response:

All API queries require a GET request using a URL of the form:

http[s]://ergast.com/api/<series>/<season>/<round>/...

where:

<series> should be set to "f1" <season> is a 4 digit integer <round> is a 1 or 2 digit integer For queries concerning a whole season, or final standings, the round element may be omitted. For example:

http://ergast.com/api/f1/2008/...

enter image description here https://ergast.com/api/f1/2020

So you are going to have to use something like beautiful soup to parse the table.

  • Related