This is my code to request data from API
`
@app.route('/quote/{symbol}')
def quote(symbol):
response = c.get_quote(symbol)
return response.json()
`
This is my json response:
{"SPY":{"assetType":"ETF","assetMainType":"EQUITY","cusip":"78462F103","assetSubType":"ETF","symbol":"SPY","description":"SPDR S&P 500","bidPrice":390.2,"bidSize":300,"bidId":"P","askPrice":390.21,"askSize":100,"askId":"P","lastPrice":389.02,"lastSize":2528800,"lastId":"P","openPrice":379.87,"highPrice":389.52,"lowPrice":379.68,"bidTick":" ","closePrice":389.02,"netChange":0.0,"totalVolume":100301958,"quoteTimeInLong":1667001600168,"tradeTimeInLong":1667001600004,"mark":389.02,"exchange":"p","exchangeName":"PACIFIC","marginable":true,"shortable":true,"volatility":0.0109,"digits":2,"52WkHigh":479.98,"52WkLow":348.11,"nAV":0.0,"peRatio":0.0,"divAmount":6.1757,"divYield":1.59,"divDate":"2022-09-16 00:00:00.000","securityStatus":"Normal","regularMarketLastPrice":389.02,"regularMarketLastSize":30,"regularMarketNetChange":0.0,"regularMarketTradeTimeInLong":1667001600004,"netPercentChangeInDouble":0.0,"markChangeInDouble":0.0,"markPercentChangeInDouble":0.0,"regularMarketPercentChangeInDouble":0.0,"delayed":false,"realtimeEntitled":true}}
How do I get the response to only be a specific part of the body like "lastPrice"?
I'm a mewbie so I have searched for a solution but cant find anything that works yet.
CodePudding user response:
You do need to parse all of the JSON you get back, but you can access individual object properties once you've done that.
Assuming your data is in a variable named data
...
console.log(data.SPY.lastPrice);
See also: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics
CodePudding user response:
Hello how are you? Basically you will take a variable that received a response.json()
and put res["SPY"]["lastPrice"]
or res.SPY.lastPrice
it will give you the value 389.02