I am using option chain output from TD Ameritrade and would like to access various values such as bid, ask, etc. and the symbol and price from underlying header.
My problem seems to be the date and strike price are keys and not fixed.
The JSON format looks as follows:
(I couldn’t post my question the json text looked like code… not sure how to post that)
The output I desire should look like this:
symbol underlyingprice putCall expdate strike bid ask
-------------------------------------------------------------------------
AMZN 90.965 PUT 2023-03-17:6 90.0 1.83 1.88
AMZN 90.965 PUT 2023-03-17:6 91.0 2.27 2.36
.
2
Answers
In the interests of brevity the JSON in the following code is a much reduced example of the JSON presented in a previous Stack Overflow question, Parse Nested JSON Data of Share Market Data, which also seems to be using data returned from Ameritrade’s Get Open Chain method:
Which yields the output:
So how does this query work? The property keys inside
putExpDateMap
are dynamic so we cannot use a hardcoded JSON path to access their contents. I use two CTE expressions to enumerate the keys:putExpDateMapKeys
enumerates the date keys, e.g.:"2021-01-08:8"
strikePriceKeys
enumerates the strike price keys, e.g.:"132.0"
and"133.0"
Each CTE expression also returns a JSON path which then allows the final invocation of
openjson()
to access theputCall
,bid
,ask
andstrikePrice
values.An alternative version to Always Learning’s great answer which doesn’t use dynamic JSON_VALUE which isn’t supported in 2016:
Here, i’m just shredding every array key and then shredding it’s content further down to get the values. I use one assumption, that strikePrice array only contains one row. If not, you can do another level of OPENJSON to get every other row