I am trying to get three specific values from within a JSON return. I can only figure out how to get ALL the information rather than specific pieces. Here’s the JSON output (note – the date 2021-08-13:2 is going to change every week but I need that value also the 710.0 will likely change, too):
{ "callExpDateMap": { "2021-08-13:2": { "710.0": [ { "ask": 8.5, "askSize": 23, "bid": 8.2, "bidAskSize": "3X23", "bidSize": 3, "closePrice": 8.28, "daysToExpiration": 2, "deliverableNote": "", "delta": 0.506, "description": "TSLA Aug 13 2021 710 Call (Weekly)", "exchangeName": "OPR", "expirationDate": 1628884800000, "expirationType": "S", "gamma": 0.019, "highPrice": 0.0, "inTheMoney": false, "isIndexOption": null, "last": 8.25, "lastSize": 0, "lastTradingDay": 1628899200000, "lowPrice": 0.0, "mark": 8.35, "markChange": 0.07, "markPercentChange": 0.88, "mini": false, "multiplier": 100.0, "netChange": -0.03, "nonStandard": false, "openInterest": 6224, "openPrice": 0.0, "optionDeliverablesList": null, "percentChange": -0.33, "putCall": "CALL", "quoteTimeInLong": 1628625599948, "rho": 0.035, "settlementType": " ", "strikePrice": 710.0, "symbol": "TSLA_081321C710", "theoreticalOptionValue": 8.277, "theoreticalVolatility": 29.0, "theta": -1.127, "timeValue": 8.25, "totalVolume": 0, "tradeDate": null, "tradeTimeInLong": 1628625596495, "vega": 0.284, "volatility": 29.119 } ] }
What I need is the date (in this case 2021-08-13:2 and also the “ask” and “bid”.
Here’s the code I’ve got so far:
for response_line in response.iter_lines(): if response_line: json_response = json.loads(response_line) #print(json.dumps(json_response, indent=1, sort_keys=True)) contract_info = json_response['callExpDateMap'] print(contract_info)
I tried things like contract_info = json_response[‘callExpDateMap’][0] thinking it’s list position 0 that would return the date but alas I’m stuck.
Thanks in advance for your help
Advertisement
Answer
Here’s a fragment based on your variable names that will find all of the ‘ask’ values:-
r = json_response['callExpDateMap'] for k in r.keys(): for m in r[k].keys(): for p in r[k][m]: print(p['ask'])