Home > Mobile >  Get value from key within nested Lists of Dicts
Get value from key within nested Lists of Dicts

Time:05-09

I am fairly new to Python and have been working on trying to print out the values attached to a key which is present in a nested list of dictionaries in a JSON. Here is the structure from what I can tell:

details - List of dicts

scorecardDetails - List of dicts

scorecard - Dict

playerHandicap - Key:Value

It is also worth noting there is another list called 'summary' at the same level as 'details'.

This is where I am at currently, but am struggling to work out how to identify that I only want to look at the 'playerHandicap' key:

details_list = json_load['details']

for index in range(len(details_list)):
    for key in details_list[index]:
        print(details_list[index][key])

Here is a snapshot of the JSON (the key/value pair required is in the 'scorecard' dict which there is multiple of):

"details":[
      {
         "startTime":"2021-03-16T12:16:16.000Z",
         "formattedStartTime":"2021-03-16T12:16:16Z",
         "scorecardDetails":[
            {
               "scorecard":{
                  "id":172482642,
                  "customerId":"******",
                  "playerProfileId":*****,
                  "roundPlayerName":"*******",
                  "connectDisplayName":"********",
                  "courseGlobalId":21042,
                  "courseSnapshotId":43716,
                  "frontNineGlobalCourseId":21042,
                  "scoreType":"STROKE_PLAY",
                  "useHandicapScoring":true,
                  "useStrokeCounting":false,
                  "startTime":"2021-03-16T12:16:16.000Z",
                  "formattedStartTime":"2021-03-16T12:16:16Z",
                  "endTime":"2021-04-23T09:09:47.000Z",
                  "formattedEndTime":"2021-04-23T09:09:47Z",
                  "unitId":"1",
                  "roundType":"ALL",
                  "inProgress":false,
                  "excludeFromStats":false,
                  "holesCompleted":18,
                  "publicRound":false,
                  "score":29,
                  "playerHandicap":0,
                  "courseHandicapStr":"061018120208141604010709111517031305",
                  "teeBox":"null",
                  "handicapType":"MEN",
                  "teeBoxRating":73.03,
                  "teeBoxSlope":118,
                  "lastModifiedDt":"2021-04-23T09:09:46.000Z",
                  "sensorOnPutter":false,
                  "handicappedStrokes":101,
                  "strokes":101,

I'm sure it's an easy solution but struggling to get my head around the different levels of looping! Thanks :)

CodePudding user response:

I am not sure to understand your question.

You can use dict.keys() to get all the keys in a dictionary.

Here is a response maybe : https://stackoverflow.com/a/7002449/19074450

dict_test = {"details":[
      {
         "startTime":"2021-03-16T12:16:16.000Z",
         "formattedStartTime":"2021-03-16T12:16:16Z",
         "scorecardDetails":[
            {
               "scorecard":{
                  "id":172482642,
                  "customerId":"6300102921355894",
                  "playerProfileId":62337607,
                  "roundPlayerName":"Greg Cetnarskyj",
                  "connectDisplayName":"5536cd4a-f35e-4034-966c-a5d8238a8c26",
                  "courseGlobalId":21042,
                  "courseSnapshotId":43716,
                  "frontNineGlobalCourseId":21042,
                  "scoreType":"STROKE_PLAY",
                  "useHandicapScoring":True,
                  "useStrokeCounting":False,
                  "startTime":"2021-03-16T12:16:16.000Z",
                  "formattedStartTime":"2021-03-16T12:16:16Z",
                  "endTime":"2021-04-23T09:09:47.000Z",
                  "formattedEndTime":"2021-04-23T09:09:47Z",
                  "unitId":"1",
                  "roundType":"ALL",
                  "inProgress":False,
                  "excludeFromStats":False,
                  "holesCompleted":18,
                  "publicRound":False,
                  "score":29,
                  "playerHandicap":0}}]}]}
                  
details_list = dict_test['details']

for index in details_list:
    for dict_ in details_list:
        for key_ in dict_.keys():
            print(dict_[key_])

CodePudding user response:

print(dict_test['details'][0]['scorecardDetails'][0]['scorecard']['playerHandicap'])

This will give you desired value which is 0.
The point is you have to be careful about what type is the 'next' value is. If it is a dictionary you should address to its elements with brackets (['details']), or if it is a list then by index ([0])
  • Related