I'm trying to implement a stock API. I have a JSON example:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "DAI.DEX",
"3. Last Refreshed": "2022-04-05",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2022-04-05": {
"1. open": "64.4900",
"2. high": "64.8200",
"3. low": "62.6200",
"4. close": "62.9600",
"5. volume": "3425810"
},
"2022-04-04": {
"1. open": "63.9900",
"2. high": "64.5400",
"3. low": "62.8100",
"4. close": "64.2600",
"5. volume": "2538008"
}
}
I'm trying to display the latest price so I always need the first element in Time Series Daily. In this example 2022-04-05. The list goes on for 20 years. I tried this:
var latestClose: String {
timeSeriesDaily.first?.value.close ?? ""
}
But every time I rerun the app it displays different values and not constantly the first value.
Here my Code:
struct StockData: Codable {
var metaData: MetaData
var timeSeriesDaily: [String: TimeSeriesDaily]
var latestClose: String {
timeSeriesDaily.first?.value.close ?? ""
}
private enum CodingKeys: String, CodingKey {
case metaData = "Meta Data"
case timeSeriesDaily = "Time Series (Daily)"
}
struct MetaData: Codable {
let information: String
let symbol: String
let lastRefreshed: String
let outputSize: String
let timeZone: String
private enum CodingKeys: String, CodingKey {
case information = "1. Information"
case symbol = "2. Symbol"
case lastRefreshed = "3. Last Refreshed"
case outputSize = "4. Output Size"
case timeZone = "5. Time Zone"
}
}
struct TimeSeriesDaily: Codable {
var open: String
var high: String
var low: String
var close: String
var volume: String
private enum CodingKeys: String, CodingKey {
case open = "1. open"
case high = "2. high"
case low = "3. low"
case close = "4. close"
case volume = "5. volume"
}
}
}
There is no first element because the object is a dictionary which is unordered by definition.
To get the most recent item you have to get the dictionary keys (this returns an array) and sort them descending. The most recent date is the first item.
var latestClose: String {
guard let mostRecentDate = timeSeriesDaily.keys.sorted(by: >).first else { return "" }
return timeSeriesDaily[mostRecentDate]!.close
}
Related
I have a simple structs to represent a JSON about a stock:
struct Share: Decodable {
enum CodingKeys: String, CodingKey {
case metaData = "Meta Data"
case prices = "Time Series (Daily)"
}
var metaData: [String: String]
var prices: [Date: TradingDay]
}
struct TradingDay: Decodable {
enum CodingKeys: String, CodingKey {
case open = "1. open"
case close = "4. close"
case high = "2. high"
case low = "3. low"
}
enum DecodingErrors: Error {
case canNotMakeDouble
case invalidCodingKeys
}
var open: Double
var close: Double
var high: Double
var low: Double
init(from decoder: Decoder) throws {
guard let container = try? decoder.container(keyedBy: CodingKeys.self) else {
throw DecodingErrors.invalidCodingKeys
}
guard let open = try? Double(container.decode(String.self, forKey: .open)),
let close = try? Double(container.decode(String.self, forKey: .close)),
let high = try? Double(container.decode(String.self, forKey: .high)),
let low = try? Double(container.decode(String.self, forKey: .low)) else {
throw DecodingErrors.canNotMakeDouble
}
self.open = open
self.close = close
self.high = high
self.low = low
}
}
JSON looks like this:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "IBM",
"3. Last Refreshed": "2022-07-01",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2022-07-01": {
"1. open": "141.0000",
"2. high": "141.6700",
"3. low": "139.2600",
"4. close": "141.1200",
"5. volume": "4012106"
},
"2022-06-30": {
"1. open": "139.5800",
"2. high": "142.4600",
"3. low": "139.2800",
"4. close": "141.1900",
"5. volume": "4878020"
},
and so on.
I'm trying to decode this with the following code:
if let (data, _) = try? await URLSession.shared.data(for: request) {
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
decoder.dateDecodingStrategy = .formatted(formatter)
do {
let share = try decoder.decode(Share.self, from: data)
print(share.metaData)
print(share.prices)
} catch {
print(error.localizedDescription)
}
}
But all I see is just an error.
When I make a prices dictionary (7th row) [String: TradingDay] type, it works appropriately.
Why doesn't it decode the dates, though I passed in the date format?
Thanks everyone.
I encountered the below message on "anonymous closure" in the TimeSeriesAdjusted.fromJson which I think may be the issue but I could not figure where may have gone wrong.
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
#0 new PriceData.fromJson (package:flutter_app_watchlist/model/TimeSeriesAdjusted.dart:60:28)
#1 new TimeSeriesAdjusted.fromJson. *****anonymous closure>** (package:flutter_app_watchlist/model/TimeSeriesAdjusted.dart:87:116)
#2 MapMixin.map (dart:collection/maps.dart:170:28)
#3 new TimeSeriesAdjusted.fromJson (package:flutter_app_watchlist/model/TimeSeriesAdjusted.dart:87:62)
#4 getData (package:flutter_app_watchlist/main.dart:21:52)
*****asynchronous suspension>**
Below is the time series json (saved as data.json in folder assets) which I would like to parse.
{
"Meta Data": {
"1. Information": "Daily Time Series with Splits and Dividend Events",
"2. Symbol": "IBM",
"3. Last Refreshed": "2021-06-07",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2021-06-07": {
"1. open": "147.55",
"2. high": "148.74",
"3. low": "147.17",
"4. close": "148.02",
"5. adjusted close": "148.02",
"6. volume": "3462712",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0"
},
"2021-06-04": {
"1. open": "146.0",
"2. high": "147.55",
"3. low": "145.76",
"4. close": "147.42",
"5. adjusted close": "147.42",
"6. volume": "3117905",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0"
},
"2021-06-03": {
"1. open": "144.91",
"2. high": "145.88",
"3. low": "144.04",
"4. close": "145.55",
"5. adjusted close": "145.55",
"6. volume": "4130741",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0"
},
"2021-06-02": {
"1. open": "144.62",
"2. high": "145.75",
"3. low": "144.11",
"4. close": "145.72",
"5. adjusted close": "145.72",
"6. volume": "2786916",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0"
},
"2021-06-01": {
"1. open": "145.0",
"2. high": "145.83",
"3. low": "143.75",
"4. close": "144.19",
"5. adjusted close": "144.19",
"6. volume": "2417455",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0"
}
}
}
Below are the codes:
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
getData() async {
String data = await rootBundle.loadString('assets/data.json');
TimeSeriesAdjusted jsonData = TimeSeriesAdjusted.fromJson(json.decode(data));
print(jsonData.toString());
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
getData();
} // main
class TimeSeriesMetaData {
final String information; // "1. Information": "Daily Time Series with Splits and Dividend Events"
final String symbol; // "2. Symbol": "IBM"
final String lastRefreshed; // "3. Last Refreshed": "2021-06-07"
final String outputSize; // "4. Output Size": "Compact"
final String timezone; // "5. Time Zone": "US/Eastern"
TimeSeriesMetaData({
required this.information,
required this.symbol,
required this.lastRefreshed,
required this.outputSize,
required this.timezone});
factory TimeSeriesMetaData.fromJson(dynamic json) {
return TimeSeriesMetaData(
information: json["1. Information"],
symbol: json["2. Symbol"],
lastRefreshed: json["3. Last Refreshed"],
outputSize: json["4. Output Size"],
timezone: json["5. Time Zone"]);
} // factory TimeSeriesMetaData.fromJson
#override
String toString() {
return 'TimeSeriesMetaData: {1. Information = $information, '
'2. Symbol = $symbol, '
'3. Last Refreshed = $lastRefreshed, '
'4. Output Size = $outputSize, '
'5. Time Zone = $timezone, }';
} // toString
} // class TimeSeriesMetaData
class PriceData {
final String open; // "1. open": "147.55"
final String high; // "2. high": "148.74"
final String low; // "3. low": "147.17"
final String close; // "4. close": "148.02"
final String adjustedClose; // "5. adjusted close": "148.02"
final String volume; // "6. volume": "3462712"
final String dividendAmount; // "7. dividend amount": "0.0000"
final String splitCoefficient; // "8. split coefficient": "1.0"
PriceData({
required this.open,
required this.high,
required this.low,
required this.close,
required this.adjustedClose,
required this.volume,
required this.dividendAmount,
required this.splitCoefficient});
factory PriceData.fromJson(dynamic json) {
return PriceData(
open: json["1. open"],
high: json["2. high"],
low: json["3. low"],
close: json["4. close"],
adjustedClose: json["5. adjustedClose"],
volume: json["6. volume"],
dividendAmount: json["7. dividendAmount"],
splitCoefficient: json["8. splitCoefficient"]);
} // factory TimeSeriesMetaData.fromJson
#override
String toString() {
return 'PriceData: {1. open = $open, 2. high = $high, '
'3. low = $low, 4. close = $close, 5. adjustedClose = $adjustedClose, '
'6. volume = $volume, 7. dividendAmount = $dividendAmount, '
'8. splitCoefficient = $splitCoefficient}';
} // toString
} // class TimeSeriesMetaData
class TimeSeriesAdjusted {
final TimeSeriesMetaData timeSeriesMetaData;
final Map<String, PriceData> timeSeriesDaily;
TimeSeriesAdjusted({
required this.timeSeriesMetaData, // security symbol e.g. IBM
required this.timeSeriesDaily, // security type e.g. common stock
});
factory TimeSeriesAdjusted.fromJson(Map<String, dynamic> json) {
return TimeSeriesAdjusted(
timeSeriesMetaData: TimeSeriesMetaData.fromJson(json["Meta Data"]),
timeSeriesDaily: Map.from(json["Time Series (Daily)"]).map((k,v) => MapEntry<String, PriceData>(k, PriceData.fromJson(v))),
);
} // fromJson - extract from Json
#override
String toString() {
//return 'TimeSeriesAdjusted: $timeSeriesMetaData';
return 'TimeSeriesAdjusted: $timeSeriesMetaData, $timeSeriesDaily';
} // toString
} // class TimeSeriesAdjusted
ah... i realised where the mistake lies.. i got the attribute name incorrect in PriceData.fromJson when i called json['xxx]. below corrected for 5, 7, 8
factory PriceData.fromJson(dynamic json) {
return PriceData(
open: json["1. open"],
high: json["2. high"],
low: json["3. low"],
close: json["4. close"],
adjustedClose: json["5. adjusted close"],
volume: json["6. volume"],
dividendAmount: json["7. dividend amount"],
splitCoefficient: json["8. split coefficient"]);
} // factory TimeSeriesMetaData.fromJson
This question already has answers here:
How can I decode a dictionary from a JSON when I don't know the keys of the dictionary? [duplicate]
(2 answers)
Closed 2 years ago.
I tried to parse JSON data from a website api in Swift, but the api is a bit weird. What works is the following: [https://www.avanderlee.com/swift/json-parsing-decoding/][1]
This is a structure to parse/translate:
{
"Meta Data": {
"1. Information": "some info",
"2. Symbol": "string symbol",
"3. Last Refreshed": "2020-03-20 16:00:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2020-03-20 16:00:00": {
"1. keyABC": "138.2700",
"2. keyCBA": "138.4900",
"3. keyCAB": "136.6500",
"4. keyACB": "136.7800",
"5. keyBAC": "3392530"
},
"2020-03-20 15:55:00": {
"1. keyABC": "137.7825",
"2. keyCBA": "139.9112",
"3. keyCAB": "137.0365",
"4. keyACB": "138.2925",
"5. keyBAC": "2463243"
},
"2020-03-20 15:50:00": {
"1. keyABC": "139.0000",
"2. keyCBA": "139.0150",
"3. keyCAB": "137.7500",
"4. keyACB": "137.7500",
"5. keyBAC": "1051283"
},
...
}
}
Problem 1: There are random-keys as values that I need and don't know how to parse without just leaving deleting
Problem 2: The JSON Libraries are not in an array instead in a new object. But the goal is to make a Swift array out of it
I wonder if there is an easy way to parse something like above with JSONDecode (and if there is one, what's the best?).
app.quicktype.io provides a pretty good starting point for parsing this JSON:
struct JSONData: Codable {
let metaData: MetaData
let timeSeries5Min: [String: TimeSeries5Min]
enum CodingKeys: String, CodingKey {
case metaData = "Meta Data"
case timeSeries5Min = "Time Series (5min)"
}
}
struct MetaData: Codable {
let the1Information, the2Symbol, the3LastRefreshed, the4Interval: String
let the5OutputSize, the6TimeZone: String
enum CodingKeys: String, CodingKey {
case the1Information = "1. Information"
case the2Symbol = "2. Symbol"
case the3LastRefreshed = "3. Last Refreshed"
case the4Interval = "4. Interval"
case the5OutputSize = "5. Output Size"
case the6TimeZone = "6. Time Zone"
}
}
struct TimeSeries5Min: Codable {
let the1KeyABC, the2KeyCBA, the3KeyCAB, the4KeyACB: String
let the5KeyBAC: String
enum CodingKeys: String, CodingKey {
case the1KeyABC = "1. keyABC"
case the2KeyCBA = "2. keyCBA"
case the3KeyCAB = "3. keyCAB"
case the4KeyACB = "4. keyACB"
case the5KeyBAC = "5. keyBAC"
}
}
Given that it should be fairly easy to extract whatever data you need into an array.
While trying to parse a time-series data I found a key field in the JSON data is the timestamp(obviously in string format). But creating a struct for the same beforehand is not possible as I cannot know the timestamp string anyway.
This is how the JSON looks like:
"Time Series (5min)": {
"2020-01-17 16:00:00": {
"1. open": "167.2000",
"2. high": "167.3400",
"3. low": "167.0100",
"4. close": "167.0500",
"5. volume": "1646699"
},
"2020-01-17 15:55:00": {
"1. open": "166.9000",
"2. high": "167.1600",
"3. low": "166.8500",
"4. close": "167.1500",
"5. volume": "622999"
},
"2020-01-17 15:50:00": {
"1. open": "166.7241",
"2. high": "166.9200",
"3. low": "166.7200",
"4. close": "166.8999",
"5. volume": "271723"
}
}
The struct for the some may look like :
type TIMESTAMP struct {
Open string `json:"1. open"`
High string `json:"2. high"`
Low string `json:"3. low"`
Close string `json:"4. close"`
Volumn string `json:"5. volumn"`
}
type TIMESERIES struct {
TimeStamp TIMESTAMP `json:""` //DON'T KNOW HOW TO IMPLEMENT THIS
}
How to handle such a situation? Is there any Go struct tag for the same?
Keys like 2020-01-17 16:00:00 seems to by dynamically generated and are not fixed, so you can use map for arbitary keys like this
package main
import (
"fmt"
"encoding/json"
)
type TIMESTAMP struct {
Open string `json:"1. open"`
High string `json:"2. high"`
Low string `json:"3. low"`
Close string `json:"4. close"`
Volumn string `json:"5. volumn"`
}
type TIMESERIES map[string]map[string]TIMESTAMP
func main() {
test := []byte(`{
"Time Series (5min)": {
"2020-01-17 16:00:00": {
"1. open": "167.2000",
"2. high": "167.3400",
"3. low": "167.0100",
"4. close": "167.0500",
"5. volume": "1646699"
},
"2020-01-17 15:55:00": {
"1. open": "166.9000",
"2. high": "167.1600",
"3. low": "166.8500",
"4. close": "167.1500",
"5. volume": "622999"
},
"2020-01-17 15:50:00": {
"1. open": "166.7241",
"2. high": "166.9200",
"3. low": "166.7200",
"4. close": "166.8999",
"5. volume": "271723"
}
}
}`)
var response TIMESERIES
if err := json.Unmarshal(test, &response); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v", response)
}
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "FB",
"3. Last Refreshed": "2017-08-23 16:00:00",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2017-08-23 16:00:00": {
"1. open": "168.8400",
"2. high": "169.3600",
"3. low": "168.2000",
"4. close": "168.7100",
"5. volume": "8198515"
},
"2017-08-22": {
"1. open": "168.2800",
"2. high": "169.8700",
"3. low": "167.1500",
"4. close": "169.6400",
"5. volume": "11333260"
},
"2017-08-21": {
"1. open": "167.1600",
"2. high": "168.0000",
"3. low": "165.8200",
"4. close": "167.7800",
"5. volume": "11880823"
},
"2017-08-18": {
"1. open": "166.8400",
"2. high": "168.6700",
"3. low": "166.2100",
"4. close": "167.4100",
"5. volume": "14933261"
},
"2017-08-17": {
"1. open": "169.3400",
"2. high": "169.8600",
"3. low": "166.8500",
"4. close": "166.9100",
"5. volume": "16791591"
},
"2017-08-16": {
"1. open": "171.2500",
"2. high": "171.3800",
"3. low": "169.2400",
"4. close": "170.0000",
"5. volume": "15580549"
}
}
}
My problem is how can I get all the information (ex: 4.close) from the JSON date if the days (ex: 2017-08-02) are constantly changing.
So far in my project I can only get a data of a certain date.
func fetchStockDataCalendar() {
let url = URL(string: "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=\(symbol)&apikey=\(apiKey)")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print ("ERROR")
} else {
if let content = data {
do {
//Array
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let Time = myJson["Time Series (Daily)"] as? NSDictionary {
if let Day = Time["2017-06-21"] as? NSDictionary {
if let CloseStockData = Day["4. close"] as? String {
print("2017-06-21 CloseStock-> \(CloseStockData)$")
}
}
}
} catch {
print(error.localizedDescription)
}
}
}
}
task.resume()
}
Here is your func with the above code but as an NSDictionary. My console is showing all of the closes.
2017-10-24 CloseStock-> 256.5600
2018-02-22 CloseStock-> 270.4000
func fetchStockDataCalendar() {
let url = URL(string: "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=\("SPY")&apikey=\(alphaApiKey)")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print ("ERROR")
} else {
if let content = data {
do {
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let time = myJson["Time Series (Daily)"] as? NSDictionary {
for (key, value) in time {
if let value = value as? Dictionary<String, String> {
if let close = value["4. close"] {
print("\(key) CloseStock-> \(close)")
}
}
}
}
} catch {
print(error.localizedDescription)
}
}
}
}
task.resume()
}
Use a for loop to iterate over the dictionary keys and values like this:
if let time = myJson["Time Series (Daily)"] {
for (key, value) in time {
if let close = value["4. close"] {
print("\(key) CloseStock-> \(close)")
}
}
}
Based on your comments I've added this too
if let time = myJson["Time Series (Daily)"] {
for (key, value) in time {
if let value = value as? Dictionary<String, String> {
if let close = value["close"] {
print("\(key) CloseStock-> \(close)")
}
}
}
}