I have a JSON string stored in a SQL Server table column. The column is named MSSG_RECIPS and contains one long text string like:
`{"type":"email","recipient":"\"Artzer, Daniel J\" <[email protected]>","sentTS":"2017-11-08T20:58:14.600Z"},{"type":"email","recipient":"\"Friedman, Brian\" <[email protected]>","sentTS":"2017-11-08T20:58:14.600Z"},{"type":"email","recipient":"\"Higgins, Laura L\" <[email protected]>","sentTS":"2017-11-08T20:58:14.600Z"},{"type":"email","recipient":"\"Landenberger, Dan R\" <[email protected]>","sentTS":"2017-11-08T20:58:14.600Z"},{"type":"email","recipient":"\"Leitl, Scott\" <[email protected]>","sentTS":"2017-11-08T20:58:14.600Z"},{"type":"email","recipient":"\"Mendoza, Mario\" <[email protected]>","sentTS":"2017-11-08T20:58:14.600Z"}`
This sample string illustrates the format of my JSON, each element being comma-separated.
I am able to parse this 2 ways. One, using JSON_VALUE, I can retrieve the attributes as individual columns:
select
JSON_VALUE(mssg_recips, '$.type'),
JSON_VALUE(mssg_recips, '$.recipient'),
JSON_VALUE(mssg_recips, '$.sentTS'),
DOC_ID
from MY_JSON_TAB
However, this only returns the 1st element of the JSON.
The other method I tried is:
select doc_id, value as EMAIL_RECIP
from MY_JSON_TAB
Cross Apply OPENJSON(MSSG_RECIPS)
This returned the record as rows instead of columns but, again, only the 1st element.
How do I traverse downward, so to speak, to retrieve the 2nd, 3rd, and so on elements?