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):
JavaScript
x
56
56
1
{
2
"callExpDateMap": {
3
"2021-08-13:2": {
4
"710.0": [
5
{
6
"ask": 8.5,
7
"askSize": 23,
8
"bid": 8.2,
9
"bidAskSize": "3X23",
10
"bidSize": 3,
11
"closePrice": 8.28,
12
"daysToExpiration": 2,
13
"deliverableNote": "",
14
"delta": 0.506,
15
"description": "TSLA Aug 13 2021 710 Call (Weekly)",
16
"exchangeName": "OPR",
17
"expirationDate": 1628884800000,
18
"expirationType": "S",
19
"gamma": 0.019,
20
"highPrice": 0.0,
21
"inTheMoney": false,
22
"isIndexOption": null,
23
"last": 8.25,
24
"lastSize": 0,
25
"lastTradingDay": 1628899200000,
26
"lowPrice": 0.0,
27
"mark": 8.35,
28
"markChange": 0.07,
29
"markPercentChange": 0.88,
30
"mini": false,
31
"multiplier": 100.0,
32
"netChange": -0.03,
33
"nonStandard": false,
34
"openInterest": 6224,
35
"openPrice": 0.0,
36
"optionDeliverablesList": null,
37
"percentChange": -0.33,
38
"putCall": "CALL",
39
"quoteTimeInLong": 1628625599948,
40
"rho": 0.035,
41
"settlementType": " ",
42
"strikePrice": 710.0,
43
"symbol": "TSLA_081321C710",
44
"theoreticalOptionValue": 8.277,
45
"theoreticalVolatility": 29.0,
46
"theta": -1.127,
47
"timeValue": 8.25,
48
"totalVolume": 0,
49
"tradeDate": null,
50
"tradeTimeInLong": 1628625596495,
51
"vega": 0.284,
52
"volatility": 29.119
53
}
54
]
55
}
56
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:
JavaScript
1
7
1
for response_line in response.iter_lines():
2
if response_line:
3
json_response = json.loads(response_line)
4
#print(json.dumps(json_response, indent=1, sort_keys=True))
5
contract_info = json_response['callExpDateMap']
6
print(contract_info)
7
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:-
JavaScript
1
6
1
r = json_response['callExpDateMap']
2
for k in r.keys():
3
for m in r[k].keys():
4
for p in r[k][m]:
5
print(p['ask'])
6