Skip to main content
capitalize JSON in title
Link
chicks
  • 2.9k
  • 3
  • 18
  • 30

Efficiently parsing jsonJSON response data in python

Efficiently parsing http requestjson response data in python

Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1443365002681982978
Source Link
jtb
  • 185
  • 1
  • 8

Efficiently parsing http request in python

I'm using this API to get information on sports-betting odds. I am then processing it in Python to discard the irrelevant information.

Here's what the response looks like:

[
    {
        "id": "9eec062ad8ed334517ab3a4be8362739",
        "sport_key": "americanfootball_nfl",
        "sport_title": "NFL",
        "commence_time": "2021-10-01T00:20:00Z",
        "home_team": "Cincinnati Bengals",
        "away_team": "Jacksonville Jaguars",
        "bookmakers": [
            {
                "key": "unibet",
                "title": "Unibet",
                "last_update": "2021-09-29T16:41:30Z",
                "markets": [
                    {
                        "key": "spreads",
                        "outcomes": [
                            {
                                "name": "Cincinnati Bengals",
                                "price": -110,
                                "point": -7.5
                            },
                            {
                                "name": "Jacksonville Jaguars",
                                "price": -110,
                                "point": 7.5
                            }
                        ]
                    },
                    {
                        "key": "totals",
                        "outcomes": [
                            {
                                "name": "Over",
                                "price": -111,
                                "point": 46.0
                            },
                            {
                                "name": "Under",
                                "price": -109,
                                "point": 46.0
                            }
                        ]
                    }
                ]
            },
...

My code:

# filter http response
odds = []

for game in resp:
    for bookmaker in game["bookmakers"]:
        if bookmaker["key"] == "betmgm":
            for bet in bookmaker["markets"]:
                if bet["outcomes"][0]["name"] == "Over":
                    over_under_line = bet["outcomes"][0]["point"]
                elif bet["outcomes"][0]["name"] == game["home_team"]:
                    spread = bet["outcomes"][0]["point"]
    odds.append(
        {
            "home_team": game["home_team"],
            "away_team": game["away_team"],
            "spread": spread,
            "total": over_under_line,
        }
    )

I have a strong feeling that this could be made more pythonic. Any suggestions?