0

I am building a stock trading bot that can trade stocks for me based on my strategies. Right now I am just setting up the stock picking feature.

I keep getting "TypeError: the JSON object must be str, bytes or bytearray, not 'dict'" or "TypeError: string indices must be integers" when I try json.loads or json.dumps

I would like to pull just the "lastPrice" from this json response. In this case lastPrice would be 135.87

This is what I got:

data = content.json()

Json Data (pulled from broker's api)

{'WBC': {'assetType': 'EQUITY', 'assetMainType': 'EQUITY', 'cusip': '92927K102', 'symbol': 'WBC', 'description': 'Wabco Holdings Inc. Common Stock', 'bidPrice': 135.76, 'bidSize': 100, 'bidId': 'K', 'askPrice': 136.1, 'askSize': 2000, 'askId': 'P', 'lastPrice': 135.87, 'lastSize': 66100, 'lastId': 'N', 'openPrice': 135.77, 'highPrice': 135.95, 'lowPrice': 135.77, 'bidTick': ' ', 'closePrice': 135.79, 'netChange': 0.08, 'totalVolume': 482625, 'quoteTimeInLong': 1579903320193, 'tradeTimeInLong': 1579908600006, 'mark': 135.87, 'exchange': 'n', 'exchangeName': 'NYSE', 'marginable': True, 'shortable': True, 'volatility': 0.006, 'digits': 2, '52WkHigh': 146.675, '52WkLow': 108.09, 'nAV': 0.0, 'peRatio': 21.29, 'divAmount': 0.0, 'divYield': 0.0, 'divDate': '', 'securityStatus': 'Normal', 'regularMarketLastPrice': 135.87, 'regularMarketLastSize': 661, 'regularMarketNetChange': 0.08, 'regularMarketTradeTimeInLong': 1579908600006, 'netPercentChangeInDouble': 0.0589, 'markChangeInDouble': 0.08, 'markPercentChangeInDouble': 0.0589, 'regularMarketPercentChangeInDouble': 0.0589, 'delayed': True}}

trying to print just 135.87

val = json.loads(data)
print(val['lastPrice'])
martineau
  • 119,623
  • 25
  • 170
  • 301
Jeff
  • 1,018
  • 1
  • 15
  • 33
  • What version of Python are you using? – martineau Jan 25 '20 at 00:08
  • 1
    It seems that `data` is already a `dict`. So try `print( data['WBC']['lastPrice'] )` – Andrej Kesely Jan 25 '20 at 00:10
  • Does this answer your question? [python JSON object must be str, bytes or bytearray, not 'dict](https://stackoverflow.com/questions/42354001/python-json-object-must-be-str-bytes-or-bytearray-not-dict) – AMC Jan 25 '20 at 04:03

1 Answers1

2

you have 'WBC' mapping to an entire dictionary, try:

val['WBC']['lastPrice']
BpY
  • 403
  • 5
  • 12