Skip to content Skip to sidebar Skip to footer

Navigating Json Table In Python

I am trying to access the team name key value and the american key value print(bv_json['outcomes'][0]['description']) the parts of the json table that I need are denoted with the

Solution 1:

It looks like your data structure is

[{[{[{[{{}}]}]}]}]

Which is a list containing a dictionary of a list of dictionaries of lists of dictionaries, which is to say it's nested and confusing.

To make it easy on yourself, I think defining some variables will help.

Let's access the first level list item, the dictionary that contains 'path'- this dict contains all the other lists of dictionaries.

full_dict = bvjson[0] # step into a list 

Looking at the data, we know that outcomes is in the 'events' list of dicts, so let's define that variable to make it easier to step into for our when we get to our ultimate answer.

events = full_dict['events'] # access dictionary value by key 

Now we have access to events, which is a list of dictionaries of lists of dictionaries.

In events, we see that 'outcomes' actually lives two steps into the 'displayGroups' value, so let's get 'displayGroups' into something useable.

display = events['displayGroups'][0] 
# 'displayGroups' is a key in the dictionary in the event list, # and it holds a list of dictionaries, so we use [0] to step # into the list to access the dicts. # Note - if there are multiple lists this will only access the first one.

Stepping in further:

markets = display['markets'][0]

outcomes = markets['outcomes'][0]

You finally have easy access to the outcomes list of dict!

description = outcomes['description'] 
price = outcomes['price']['american']

So remember, anytime you get a confusing nested json like this, stepping in to each value can help you figure out how to get what you want and if you need to access via index (if it's a list) or via key (if it's a dictionary).

Think of all of this as just a way to diagnose and figure out why you aren't getting the values you are requesting - it will be different for each case, and different logic will be required for handling getting multiple values out of each list or dict - but this is a good start and way to get your mind around it.

Here is your data properly enclosed:

bvjson =
[
  {
    "path": [
      {
        "id": "2958468",
        "link": "/basketball/nba",
        "description": "NBA",
        "type": "LEAGUE",
        "sportCode": "BASK",
        "order": 1,
        "leaf": True,
        "current": True
      },
      {
        "id": "227",
        "link": "/basketball",
        "description": "Basketball",
        "type": "SPORT",
        "sportCode": "BASK",
        "order": 1,
        "leaf": False,
        "current": False
      }
    ],
    "events": [
      {
        "id": "8801181",
        "description": "L.A. Clippers @ Utah Jazz",
        "type": "GAMEEVENT",
        "link": "/basketball/nba/l-a-clippers-utah-jazz-202106082215",
        "status": "O",
        "sport": "BASK",
        "startTime": 1623204900000,
        "live": True,
        "awayTeamFirst": True,
        "denySameGame": "NO",
        "teaserAllowed": True,
        "competitionId": "2958468",
        "notes": "Best of 7 - Game 1",
        "numMarkets": 34,
        "lastModified": 1623212024024,
        "competitors": [
          {
            "id": "8801181-285",
            "name": "Utah Jazz",
            "home": True
          },
          {
            "id": "8801181-310",
            "name": "L.A. Clippers",
            "home": False
          }
        ],
        "displayGroups": [
          {
            "id": "100-97",
            "description": "Game Lines",
            "defaultType": True,
            "alternateType": False,
            "markets": [
              {
                "id": "157658380",
                "descriptionKey": "Head To Head",
                "description": "Moneyline",
                "key": "2W-12",
                "marketTypeId": "3059",
                "status": "O",
                "singleOnly": False,
                "notes": "",
                "period": {
                  "id": "341",
                  "description": "Live Game",
                  "abbreviation": "G",
                  "live": True,
                  "main": True
                },
                "outcomes": [
                  {
                    "id": "849253180",
                    "description": "L.A. Clippers",##############"status": "O",
                    "type": "A",
                    "competitorId": "8801181-310",
                    "price": {
                      "id": "7927852247",
                      "american": "+125",#########################"decimal": "2.250",
                      "fractional": "5/4",
                      "malay": "-0.80",
                      "indonesian": "1.25",
                      "hongkong": "1.25"}
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

Post a Comment for "Navigating Json Table In Python"