Utilizing the AlphaVantage API, I am receiving a JSON response that looks like this:
{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "IBM",
"3. Last Refreshed": "2024-04-19 19:55:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2024-04-19 19:55:00": {
"1. open": "181.5200",
"2. high": "181.5800",
"3. low": "181.4600",
"4. close": "181.5100",
"5. volume": "158"
},
"2024-04-19 19:40:00": {
"1. open": "181.2000",
"2. high": "181.2000",
"3. low": "181.2000",
"4. close": "181.2000",
"5. volume": "27"
},
"2024-04-19 19:30:00": {
"1. open": "181.5600",
"2. high": "181.5600",
"3. low": "181.5600",
"4. close": "181.5600",
"5. volume": "20"
},
"2024-04-19 19:25:00": {
"1. open": "181.5600",
"2. high": "181.5600",
"3. low": "181.5600",
"4. close": "181.5600",
"5. volume": "1"
},...
}
}
I am attempting to extract the data and save it in an object utilizing an interface:
type IntradayInterface = {
metaData: MetaData;
interval: { [time: string]: IntervalData };
}
export type MetaData = {
information: string;
symbol: string;
lastRefreshed: Date;
interval: string;
outputSize: string;
timeZone: string;
}
export type IntervalData = {
open: string;
high: string;
low: string;
close: string;
volume: string;
}
export default IntradayInterface;
Below is my parsing function:
function parseIntradayData(jsonData: any) : IntradayInterface {
const metaData = jsonData['Meta Data'];
let parsedMetaData : MetaData = {
information: metaData['1. Information'],
symbol: metaData['2. Symbol'],
lastRefreshed: metaData['3. Last Refreshed'],
interval: metaData['4. Interval'],
outputSize: metaData['5. Output Size'],
timeZone: metaData['6. Time Zone']
};
const intervalData = jsonData['Time Series (5min)'];
let parsedIntervalData: IntervalData = {
open: intervalData['1. open'],
high: intervalData['2. high'],
low: intervalData['3. low'],
close: intervalData['4. close'],
volume: intervalData['5. volume']
}
const returnData: IntradayInterface = {
metaData: parsedMetaData,
interval: intervalData
}
return returnData;
}
export default parseIntradayData;
The aim is to parse and store the data from the JSON response into an object of type IntradayInterface
, but when attempting to do so, the object doesn't populate correctly. There are two assumptions on why this might be happening:
1 - The Interface may not be structured correctly, particularly with the interval
definition. I want to include each datetime mapping to the corresponding IntervalData
, but unsure how.
2 - The parsing of the "Time Series (5min)" section of the JSON may not be done correctly.
Support or guidance on this matter would be highly appreciated!