Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

json: cannot unmarshal string into Go struct field Quote.quote.latestPrice of type float64 #64

Closed
xaionaro opened this issue Aug 23, 2020 · 2 comments · Fixed by #85
Closed

Comments

@xaionaro
Copy link

When I try to execute:

client.Book(context.Background(), "ADABNB")

I receive error:

json: cannot unmarshal string into Go struct field Quote.quote.latestPrice of type float64

The original message from IEXCloud is:

'{"quote":{"symbol":"ADABNB","primaryExchange":"","sector":"cryptocurrency","calculationPrice":"realtime","latestPrice":"0.00556100","latestSource":"Real time price","latestUpdate":1598182915533,"latestVolume":"1831991.00000000","bidPrice":"0.00555900","bidSize":"225.00000000","askPrice":"0.00556800","askSize":"3326.00000000","high":"0.00574100","low":"0.00550000","previousClose":"0.00562200","isUSMarketOpen":false},"bids":[],"asks":[],"systemEvent":{}}'
@matthewrankin
Copy link
Contributor

Looks like the symbol ADABNB doesn't return all the fields. We should probably update the code to handle JSON response with only partial data.

JSON response for /stock/adabnb/book

{
  "quote": {
    "symbol": "ADABNB",
    "primaryExchange": "",
    "sector": "cryptocurrency",
    "calculationPrice": "realtime",
    "latestPrice": "0.00553000",
    "latestSource": "Real time price",
    "latestUpdate": 1598281269671,
    "latestVolume": "1018435.00000000",
    "bidPrice": "0.00552700",
    "bidSize": "427.00000000",
    "askPrice": "0.00554200",
    "askSize": "701.00000000",
    "high": "0.00565000",
    "low": "0.00547000",
    "previousClose": "0.00555800",
    "isUSMarketOpen": true
  },
  "bids": [],
  "asks": [],
  "systemEvent": {
    "systemEvent": "R",
    "timestamp": 1598275800002
  }
}

JSON response for /stock/aapl/book

{
  "quote": {
    "symbol": "AAPL",
    "companyName": "Apple, Inc.",
    "primaryExchange": "NASDAQ",
    "calculationPrice": "tops",
    "open": null,
    "openTime": null,
    "openSource": "official",
    "close": null,
    "closeTime": null,
    "closeSource": "official",
    "high": 515.14,
    "highTime": 1598280111964,
    "highSource": "15 minute delayed price",
    "low": 495.98,
    "lowTime": 1598280888059,
    "lowSource": "IEX real time price",
    "latestPrice": 497.9,
    "latestSource": "IEX real time price",
    "latestTime": "10:56:49 AM",
    "latestUpdate": 1598281009990,
    "latestVolume": 37141817,
    "iexRealtimePrice": 497.9,
    "iexRealtimeSize": 100,
    "iexLastUpdated": 1598281009990,
    "delayedPrice": null,
    "delayedPriceTime": null,
    "oddLotDelayedPrice": null,
    "oddLotDelayedPriceTime": null,
    "extendedPrice": null,
    "extendedChange": null,
    "extendedChangePercent": null,
    "extendedPriceTime": null,
    "previousClose": 497.48,
    "previousVolume": 84513660,
    "change": 0.42,
    "changePercent": 0.00084,
    "volume": 37141817,
    "iexMarketPercent": 0.014940141458345994,
    "iexVolume": 554904,
    "avgTotalVolume": 41771726,
    "iexBidPrice": 495.5,
    "iexBidSize": 100,
    "iexAskPrice": 510,
    "iexAskSize": 17600,
    "iexOpen": null,
    "iexOpenTime": null,
    "iexClose": 497.9,
    "iexCloseTime": 1598281009990,
    "marketCap": 2128836177000,
    "peRatio": 37.51,
    "week52High": 515.14,
    "week52Low": 201,
    "ytdChange": 0.68014,
    "lastTradeTime": 1598281009990,
    "isUSMarketOpen": true
  },
  "bids": [
    {
      "price": 495.5,
      "size": 100,
      "timestamp": 1598280600036
    },
    {
      "price": 495.24,
      "size": 700,
      "timestamp": 1598280429476
    },
    {
      "price": 495,
      "size": 200,
      "timestamp": 1598275816357
    },
    {
      "price": 494,
      "size": 100,
      "timestamp": 1598275816296
    },
    {
      "price": 493.25,
      "size": 500,
      "timestamp": 1598280435173
    },
    {
      "price": 492.51,
      "size": 100,
      "timestamp": 1598278077902
    },
    {
      "price": 490,
      "size": 500,
      "timestamp": 1598275816263
    },
    {
      "price": 485,
      "size": 100,
      "timestamp": 1598275816291
    },
    {
      "price": 480,
      "size": 800,
      "timestamp": 1598275816271
    },
    {
      "price": 477,
      "size": 800,
      "timestamp": 1598275816327
    },
    {
      "price": 470.01,
      "size": 300,
      "timestamp": 1598278088371
    },
    {
      "price": 264.18,
      "size": 100,
      "timestamp": 1598275801004
    }
  ],
  "asks": [
    {
      "price": 510,
      "size": 17600,
      "timestamp": 1598277468179
    }
  ],
  "trades": [
    {
      "price": 497.9,
      "size": 100,
      "tradeId": 505697090,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598281009990
    },
    {
      "price": 497.89,
      "size": 100,
      "tradeId": 505684481,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598281009917
    },
    {
      "price": 497.805,
      "size": 100,
      "tradeId": 504962637,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598281002084
    },
    {
      "price": 497.82,
      "size": 100,
      "tradeId": 504894401,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598281000992
    },
    {
      "price": 497.83,
      "size": 100,
      "tradeId": 504894143,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598281000985
    },
    {
      "price": 497.95,
      "size": 100,
      "tradeId": 504448190,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280994471
    },
    {
      "price": 497.95,
      "size": 200,
      "tradeId": 504356511,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280993244
    },
    {
      "price": 497.93,
      "size": 100,
      "tradeId": 504207080,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280990619
    },
    {
      "price": 497.93,
      "size": 3,
      "tradeId": 504191435,
      "isISO": false,
      "isOddLot": true,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280990323
    },
    {
      "price": 497.765,
      "size": 10,
      "tradeId": 503771386,
      "isISO": false,
      "isOddLot": true,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280985170
    },
    {
      "price": 497.91,
      "size": 100,
      "tradeId": 503660724,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280983747
    },
    {
      "price": 497.91,
      "size": 100,
      "tradeId": 503397499,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280980297
    },
    {
      "price": 497.72,
      "size": 50,
      "tradeId": 502724027,
      "isISO": false,
      "isOddLot": true,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280973745
    },
    {
      "price": 497.7,
      "size": 100,
      "tradeId": 502723974,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280973745
    },
    {
      "price": 497.65,
      "size": 50,
      "tradeId": 502723920,
      "isISO": false,
      "isOddLot": true,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280973745
    },
    {
      "price": 497.56,
      "size": 24,
      "tradeId": 502335984,
      "isISO": false,
      "isOddLot": true,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280969960
    },
    {
      "price": 497.56,
      "size": 1,
      "tradeId": 502320065,
      "isISO": false,
      "isOddLot": true,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280969665
    },
    {
      "price": 497.52,
      "size": 100,
      "tradeId": 502312249,
      "isISO": true,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280969545
    },
    {
      "price": 497.745,
      "size": 100,
      "tradeId": 501434089,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280962024
    },
    {
      "price": 497.91,
      "size": 100,
      "tradeId": 501275956,
      "isISO": false,
      "isOddLot": false,
      "isOutsideRegularHours": false,
      "isSinglePriceCross": false,
      "isTradeThroughExempt": false,
      "timestamp": 1598280961029
    }
  ],
  "systemEvent": {
    "systemEvent": "R",
    "timestamp": 1598275800002
  }
}

@lfaoro
Copy link

lfaoro commented Aug 19, 2021

you can add omitempty to the fields which might end up to be partial or just add omitempty to the whole struct in order to be able to handle any mixed response.

e.g.

// Quote models the data returned from the IEX Cloud /quote endpoint.
type Quote struct {
	Symbol                string    `json:"symbol,omitempty"

lfaoro added a commit to lfaoro/iexcloud that referenced this issue Aug 19, 2021
fix goinvest#64

certain symbols don't return all the data e.g. "ADABNB"

using the 'omitempty' tag, the unmarshaler will not error out if a field is missing

Signed-off-by: Leonardo Faoro <lfaoro@gmail.com>
lfaoro added a commit to lfaoro/iexcloud that referenced this issue Aug 19, 2021
fix goinvest#64

certain symbols don't return all the data e.g. "ADABNB"

using the 'omitempty' tag, the unmarshaler will not error out if a field is missing

Signed-off-by: Leonardo Faoro <lfaoro@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants