The AlphaVantage API has spaces and periods in the keys. There is no formal doco for the API, though you can see it in their demo url
In my Typescript app I created data structs for this (I'm happy for anyone to copy and use these - perhaps after the solution to my question is found):
export class MetaData {
'1. Information': string
'2. Symbol': string
'3. Last Refreshed': string
'4. Output Size': string
'5. Time Zone': string
constructor(one, two, three, four, five) {
this['1. Information'] = one
this['2. Symbol'] = two
this['3. Last Refreshed'] = three
this['4. Output Size'] = four
this['5. Time Zone'] = five
}
}
export interface TimeSeries {
[date: string]: {
'1. open': string;
'2. high': string;
'3. low': string;
'4. close': string;
'5. volume': string;
}
}
export interface AlphaVantage {
'Meta Data': MetaData;
'Time Series (Daily)'?: TimeSeries;
'Time Series (Weekly)'?: TimeSeries;
}
I call the API using alphavantage
from NPM and implicitly cast it to my AlphaVantage
:
const av: AlphaVantage = await alpha.data.weekly(options.stocks, 'compact', 'json')
And then (potentially after some massaging etc) I persist it in a MongoDB collection:
const doc = await this.model.findByIdAndUpdate(proxyModel._id, proxyModel)
(The ProxyModel is a DTO used to define database keys such as date, stock symbol etc... One of the fields is the AlphaVantage data).
This must serialize the data and it errors with:
key 1. Information must not contain '.'
Is there an easy way to handle this. My choice would be to create equivalent objects without spaces:
export interface TimeSeries {
[date: string]: {
'1_open': string;
'2_high': string;
'3_low': string;
'4_close': string;
'5_volume': string;
}
}
And then cast to this. In which case provide a mapping ...
I can see my self creating an implementation. However before I get in to this I'd like to hear of any ideas how best to handle this data structure.