我目前能够使用SQL Server" OPENJSON WITH解析大部分JSON文件(..."语法。但是,这个特定文件包含我不知道的嵌套数组如何处理。
我读过的很多例子都将JSON引用为变量。在这种情况下,我正在调用一个文件:
select DEV_JSON.*
from OPENROWSET
(BULK 'C:\Users\Myuser\Documents\JSON_extract.json', SINGLE_CLOB) as my_datafile
CROSS APPLY OPENJSON(BulkColumn)
WITH
(DOC_ID varchar(100) '$.doc._id',
DOC_REV varchar(45) '$.doc._rev',
DELY_APPL_NAME varchar(20) '$.doc.delivery.application',
DELY_SENT_BY varchar(25) '$.doc.delivery.sender.id',
DELY_SENT_TYPO varchar(20) '$.doc.delivery.sender.type',
.....
....
...
..) as DEV_JSON
其中一个属性包含嵌套数组。下面我复制了我的JSON的前5个属性,以及嵌套的"收件人"阵列。
如何构建我的SQL以解析此部分?
"doc": {
"_id": "049d4e4030afcdeefedaa90f640f91d4a2be93d7-bd_abcxyz@somemail.com",
"_rev": "3-e119db13dae8d50ae0c4579ba9c87fc9",
"delivery": {
"application": "App_XYZ",
"sender": {
"id": "MABarrera@yahoo.com",
"type": "user"
},
"recipients": [{
"type": "email",
"recipient": "\"Artzer, Daniel J\" <DJArtzer@emailaddr.com>",
"sentTS": "2017-10-18T13:04:00.133Z"
},
{
"type": "email",
"recipient": "\"Higgins, Laura L\" <LLHiggins@emailaddr.com>",
"sentTS": "2017-10-18T13:04:00.133Z"
},
{
"type": "email",
"recipient": "\"Friedman, Brian\" <BFriedman@emailaddr.com>",
"sentTS": "2017-10-18T13:04:00.133Z"
},
{
"type": "email",
"recipient": "\"Garcia, Charlie M\" <CMGarcia@emailaddr.com>",
"sentTS": "2017-10-18T13:04:00.133Z"
}
]
},
答案 0 :(得分:9)
我遇到了同样的问题,最后我用多个CROSS APPLY子句解决了这个问题。
以下是我的JSON示例:
SELECT
a.AppId
,[Permission] = c.Value
,b.PermissionTypeID
FROM
OPENJSON(@PermsJSON)
WITH
(
AppId INT N'$.AppId'
,Perms NVARCHAR(MAX) AS JSON
) AS a
CROSS APPLY
OPENJSON(a.Perms)
WITH
(
PermissionTypeID INT
,[Permission] NVARCHAR(MAX) AS JSON
) AS b
CROSS APPLY OPENJSON(b.Permission) AS c;
然后我可以使用以下查询解析它:
AppId Permission PermissionTypeID
1 AA 2
1 BB 2
1 10 1
2 IM 2
2 NM 2
2 42 1
结果如下:
@ECHO OFF
set /p "dir=Specify Path:"
CALL set "dir=%dir%"
if exist "%dir%" (
echo ok
) else (
echo not ok
)
答案 1 :(得分:1)
在 MUCH 搜索之后,我终于找到了这个问题的答案。我只需要在我的查询中包含嵌套数组作为另一个JSON列,例如:
WITH
(DOC_ID varchar(100) '$.doc._id',
DOC_REV varchar(45) '$.doc._rev',
DELY_APPL_NAME varchar(20) '$.doc.delivery.application',
DELY_SENT_BY varchar(25) '$.doc.delivery.sender.id',
DELY_SENT_TYPO varchar(20) '$.doc.delivery.sender.type',
RECIPS nvarchar(max) '$.doc.delivery.recipients' as JSON,
PAYLOAD_START_TIME varchar(30) '$.doc.payload.startTS',
....
...
..
) as my_query
因此,我最终得到每个JSON文档一条记录,其中(在本例中)是一个包含JSON文本的varchar列。
接下来,我可以在此列上运行单独的查询来解析JSON并创建一个&#34;子表&#34;与父母相关联。