I am currently trying to figure out how to parse JSON information in Python and am having a harder time than I think I should.
Below is a sample of the information I am trying to parse.
{
"2019-05-09": {
"1. open": "124.2900",
"2. high": "125.7800",
"3. low": "123.5700",
"4. close": "125.5000",
"5. volume": "23491093"
},
"2019-05-08": {
"1. open": "125.4400",
"2. high": "126.3700",
"3. low": "124.7500",
"4. close": "125.5100",
"5. volume": "25775583"
},
"2019-05-07": {
"1. open": "126.4600",
"2. high": "127.1800",
"3. low": "124.2200",
"4. close": "125.5200",
"5. volume": "36017661"
}
}
I am trying to store each day into a class so that I can parse the information.
In the below example, I am only trying to print the opening for these records. According to the simple examples I've looked at, this should work but always comes up with the error 'string indices must be integers.'
from alpha_vantage.timeseries import TimeSeries
import json
class day_history:
def __init__(self, date, open, high, low, close, volume):
self.date = date
self.open = open
self.high = high
self.low = low
self.close = close
self.volume = volume
alpha_adv_key = "aaaaaaaaaaa"
ts = TimeSeries(key=alpha_adv_key)
data, meta_data = ts.get_daily(symbol='MSFT')
results = json.dumps(data)
for day in results:
print(day["1. open"])
What is the correct way to parse this JSON data so that I can store it in the class?