0

I'm trying to loop through a json response object that contains data. I would like to loop thought the json object and extract the for keys event-id, market-id, and event-participant-name. The json response is as below

[{
  "TIMESTAMP": "2018-09-05 22: 59: 44.398534 ",
  "id": 900652866170042,
  "name": "C Suarez Navarro vs M Keys",
  "sport-id": 9,
  "start": "2018-09-05T23:10:00.000Z",
  "in-running-flag": false,
  "allow-live-betting": true,
  "category-id": [
    9,
    399952692940010,
    410468520880009,
    573630974180009,
    613128376040013,
    643136938410012,
    894084819950041
  ],
  "status": "open",
  "volume": 83821.22796,
  "event-participants": [{
      "id": 900652866280041,
      "event-id": 900652866170042,
      "participant-name": "C Suarez Navarro",
      "number": 1
    },
    {
      "id": 900652866290042,
      "event-id": 900652866170042,
      "participant-name": "M Keys",
      "number": 2
    }
  ],
  "markets": [{
        "live": false,
        "event-id": 900652866170042,
        "id": 900652866490041,
        "name": "Moneyline",
        "runners": [{
              "withdrawn": false,
              "prices": [{
                  "available-amount": 1390.32516,
                  "currency": "EUR",
                  "odds-type": "DECIMAL",
                  "odds": 3.44,
                  "decimal-odds": 3.44,
                  "side": "back",
                  "exchange-type": "back-lay"
                },
                {
                  "available-amount": 12.22,
                  "currency": "EUR",
                  "odds-type": "DECIMAL",
                  "odds": 3.36,
                  "decimal-odds": 3.36,
                  "side": "back",
                  "exchange-type": "back-lay"
                },
                {
                  "available-amount": 38.84366,
                  "currency": "EUR",
                  "odds-type": "DECIMAL",
                  "odds": 3.34,
                  "decimal-odds": 3.34,
                  "side": "back",
                  "exchange-type": "back-lay"
                },
                {
                  "available-amount": 1843.65097,
                  "currency": "EUR",
                  "odds-type": "DECIMAL",
                  "odds": 3.48,
                  "decimal-odds": 3.48,
                  "side": "lay",
                  "exchange-type": "back-lay"
                },
                {
                  "available-amount": 27.82505,
                  "currency": "EUR",
                  "odds-type": "DECIMAL",
                  "odds": 3.5,
                  "decimal-odds": 3.5,
                  "side": "lay",
                  "exchange-type": "back-lay"
                },
                {
                  "available-amount": 11.20312,
                  "currency": "EUR",
                  "odds-type": "DECIMAL",
                  "odds": 3.56,
                  "decimal-odds": 3.56,
                  "side": "lay",
                  "exchange-type": "back-lay"
                }
              ],
              "event-id": 900652866170042,
              "id": 900652866590042,
              "market-id": 900652866490041,
              "name": "C Suarez Navarro",
              "status": "open",
              "volume": 25342.31304,
              "event-participant-id": 900652866280041
            },
            {
              "withdrawn": false,
              "prices": [{
                    "available-amount": 4572.25441,
                    "currency": "EUR",
                    "odds-type": "DECIMAL",
                    "odds": 1.40322,
                    "decimal-odds": 1.40322,
                    "side": "back",
                    "exchange-type": "back-lay"
                  },
                  {
                    "available-amount": 69.56263,
                    "currency": "EUR",
                    "odds-type": "DECIMAL",
                    "odds": 1.4,
                    "decimal-odds": 1.4,
                    "side": "back",
                    "exchange-type": "back-lay"
                  },

When I loop thought this JSON object I get a key error. How do I loop though this object and return every value for each attribute?

Code

def match_book_get(self):

   tennis_events = self.api.market_data.get_events(sport_ids= 
   [9],states=MarketStates.All, per_page=10000, offset=0,
                                include_event_participants=Boolean.T, 
   price_depth=3, side=Side.All)

   data = []

   for data in tennis_events:
       event_id = data['event_id']
       market_id = data['market-id']
       evparid = data['event-participant-id']
0

1 Answer 1

1

As can be seen from the JSON response, the keys you are looking for are located in a list under the markets and runners keys. You should use something like the following:

for data in tennis_events:
    for market in data['markets']:
       event_id = market['event-id']
       for runner in market['runners']:
           market_id = runner['market-id']
           evparid = runner['event-participant-id']
Sign up to request clarification or add additional context in comments.

6 Comments

You should use a dash instead of an underscore (i.e. event-id instead of event_id)
Cheers that worked. I'm getting a keyerror on event-participant-id I'm not sure why? would it be in a different section of the json object?
You posted an incomplete JSON response but maybe not every runner block has an event-participant-id?
Here's the gist to the full json. It's really weird since you say that some runner blocks has event-paricipant-id and some don't gist.github.com/Hey-tom/46f96552439b084f9efdc6239cf23084
It looks like that is your problem. If you can substitute a default value when there is none, you can use runner.get('event-participant-id', 'my_default_value') to prevent exceptions.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.