1

I have json document as below. I would like to update all "points" to type integer. I'm familair with how to typecast. However I'm not able to reach to each indiviaul key/value pair inside the key "score". Would you please help me guide with the concept here

{"playerID": "123",
"score": [{"date": "1/1/2019", 
           "points": "10", 
            "somekey": "somevalue"
           }, 
           {"date": "1/1/2018", 
           "points": "100", 
            "somekey": "valuexyz"
           }]
}

I tried to read the json data into variable called "data" data.get("score") returned a "list" of items item(0) gets me entire record - date, points, somekey. I'm not able to get to a specific key called points.

Should I onceagain convert my list to dictionary and then iterate to get to points? Isn't there any other way

data = {"playerID": "123",
"score": [{"date": "1/1/2019", 
       "points": "10", 
        "somekey": "somevalue"
       }, 
       {"date": "1/1/2018", 
       "points": "100", 
        "somekey": "valuexyz"
       }]
}
dlist = data.get("score") #returns list
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
flowgician
  • 63
  • 1
  • 1
  • 7
  • See this for a discussion of how to flatten nested dicts, it might be helpful: https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys – neutrino_logic Aug 31 '19 at 00:20

1 Answers1

1
for x in data['score']:
    x['points'] = int(x['points'])
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135