how to loop through json response object with python?Ask Questions

 Posted on 06/09/2018 7:58:54 AM

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-idmarket-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']

Share On: facebook gplus twitter
profile
Asked by Hardik Chaudhary on 06/09/2018 7:58:54 AM Score: 366 points
Add Comment:
Submit

Comments

0 Answer

0 Corrected Answers

Post Your Answer

  •  
  •  

Existing Members

Sign in to your account
Email Address
Password
...or Join us
Download, Vote, Comment, Publish.
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.