I use an api to obtain information on a particular share.
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2020-05-22",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-05-22": {
"1. open": "183.1900",
"2. high": "184.4600",
"3. low": "182.5400",
"4. close": "183.5100",
"5. volume": "20826898"
},
"2020-05-21": {
"1. open": "185.4000",
"2. high": "186.6700",
"3. low": "183.2900",
"4. close": "183.4300",
"5. volume": "29032741"
}, and more...
I would now like to extract only the date, open,high to convert it into a CSV.
import requests
import json
url = "https://alpha-vantage.p.rapidapi.com/query"
querystring = {"outputsize":"compact","datatype":"JSON","function":"TIME_SERIES_DAILY","symbol":"MSFT"}
headers = {
'x-rapidapi-host': "alpha-vantage.p.rapidapi.com",
'x-rapidapi-key': "API KEY"
}
response = requests.request("GET", url, headers=headers, params=querystring)
info = response.json()
with open('data.json', 'w') as fp:
json.dump(info, fp)
f = open('data.json',)
data = json.load(f)
meta = data["Meta Data"]
for i in data['Meta Data']:
print(i)
# Closing file
f.close()
output:
1. Information
2. Symbol
3. Last Refreshed
4. Output Size
5. Time Zone
I thought the information was in "Meta Data" but apparently not. Can someone explain to me exactly what I'm doing wrong?