diogenes
diogenes

Reputation: 2119

Swift JSON Search Date format by Year-month?

I am using Eodhistoricaldata.com's API to get the monthly values of issues.

https://eodhistoricaldata.com/api/eod/VTSMX?from=2017-09-01&api_token=xxxxxx&period=m&fmt=json

And even though it is monthly data they are assigning the first trade date to the results. -01, -02, -03, etc

This means I can not use a generic date of -01. So YYYY-MM-01 does not work.

So I either have to change all the dates to -01 or search by just the year and month like "2017-10"

What is the best way to accomplish this using Swift 4 and SwiftyJSON.

Thanks.

Here is their data.

[{"date":"2017-09-01","open":"61.9300","high":63.03,"low":"61.4400","close":63.03,"adjusted_close":61.6402,"volume":0},
{"date":"2017-10-02","open":"63.3400","high":"64.5300","low":"63.3400","close":64.39,"adjusted_close":62.9703,"volume":0},
{"date":"2017-11-01","open":"64.4400","high":66.35,"low":"64.0600","close":66.35,"adjusted_close":64.8872,"volume":0},
{"date":"2017-12-01","open":"66.2100","high":"67.3500","low":"65.7700","close":66.7,"adjusted_close":65.5322,"volume":0},
{"date":"2018-01-02","open":"67.2500","high":"71.4800","low":"67.2500","close":70.24,"adjusted_close":69.0102,"volume":0},
{"date":"2018-02-01","open":"70.2400","high":"70.2400","low":"64.4000","close":67.63,"adjusted_close":66.4458,"volume":0},
....
{"date":"2018-12-03","open":"69.5700","high":"69.5700","low":"58.1700","close":62.08,"adjusted_close":62.08,"volume":0}]

Upvotes: 0

Views: 238

Answers (2)

diogenes
diogenes

Reputation: 2119

I was able to work with the API company to overcome this issue with eodhistorical data.

They now have written a special API to get back just the last monthly data - so no more need to write crazy Json code. There is now a special request for the data using "period = eom"

jsonUrl = "https://eodhistoricaldata.com/api/eod/(symbol).US?from=(beg)&to=(end)&api_token=(EOD_KEY)&period=eom&fmt=json"

this really made live much better. (After wasting house trying to overcome their data.)

Upvotes: 0

vadian
vadian

Reputation: 285072

Drop SwiftyJSON and use Decodable to parse the JSON into a struct. Parsing dates is highly customizable. You can add your own logic which extracts year and month from the date string and create a Date instance.

struct HistoricalData: Decodable {
    let date: Date
    let open, low, high : String
    let close, adjustedClose, volume : Double
}

...

let jsonString = """
[{"date":"2017-09-01","open":"61.9300","high":"63.03","low":"61.4400","close":63.03,"adjusted_close":61.6402,"volume":0},
{"date":"2017-10-02","open":"63.3400","high":"64.5300","low":"63.3400","close":64.39,"adjusted_close":62.9703,"volume":0},
{"date":"2017-11-01","open":"64.4400","high":"66.35","low":"64.0600","close":66.35,"adjusted_close":64.8872,"volume":0},
{"date":"2017-12-01","open":"66.2100","high":"67.3500","low":"65.7700","close":66.7,"adjusted_close":65.5322,"volume":0},
{"date":"2018-01-02","open":"67.2500","high":"71.4800","low":"67.2500","close":70.24,"adjusted_close":69.0102,"volume":0},
{"date":"2018-02-01","open":"70.2400","high":"70.2400","low":"64.4000","close":67.63,"adjusted_close":66.4458,"volume":0},
{"date":"2018-12-03","open":"69.5700","high":"69.5700","low":"58.1700","close":62.08,"adjusted_close":62.08,"volume":0}]
"""

let data = Data(jsonString.utf8)

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .custom { decoder -> Date in
    let container = try decoder.singleValueContainer()
    let dateStr = try container.decode(String.self)
    let components = dateStr.components(separatedBy: "-")
    guard components.count > 2, let year = Int(components[0]), let month = Int(components[1]) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date string") }
    return Calendar.current.date(from: DateComponents(year: year, month: month))!
}
do {
    let result = try decoder.decode([HistoricalData].self, from: data)
    print(result)
} catch { print(error) }

Alternatively you can decode the string to format yyyy-MM however you have to write an initializer and add CodingKeys

struct HistoricalData: Decodable {
    let date: String
    let open, low, high : String
    let close, adjustedClose, volume : Double

    private enum CodingKeys : String, CodingKey {
       case date, open, low, high, close, adjustedClose, volume
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let dateString = try container.decode(String.self, forKey: .date)
        guard let secondDashRange = dateString.range(of: "-", options: .backwards) else {
            throw DecodingError.dataCorruptedError(forKey: .date, in: container, debugDescription: "Invalid date string")
        }
        date = String(dateString[..<secondDashRange.lowerBound])
        open = try container.decode(String.self, forKey: .open)
        low = try container.decode(String.self, forKey: .low)
        high = try container.decode(String.self, forKey: .high)
        close = try container.decode(Double.self, forKey: .close)
        adjustedClose = try container.decode(Double.self, forKey: .adjustedClose)
        volume = try container.decode(Double.self, forKey: .volume)
    }

}

let data = Data(jsonString.utf8)

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
    let result = try decoder.decode([HistoricalData].self, from: data)
    print(result)
} catch { print(error) }

Upvotes: 2

Related Questions