我在JSON文件中具有以下代码:
{
"took": 196,
"timed_out": false,
"_shards": {
"total": 15,
"successful": 15,
"failed": 0
},
"hits": {
"total": 165,
"max_score": null,
"hits": [
{
"_index": "logstash-2018.11.22",
"_type": "nagios_core",
"_id": "AWc6C_EtHRYvW4hmI7sl",
"_score": null,
"_source": {
"message": "EXTERNAL COMMAND: ACKNOWLEDGE_SVC_PROBLEM;DE-Hoeheinoed-VOC1-SRV;ntp_timesync;2;0;0;Jaizel Jem Perdon;SN 307185410",
"@version": "1",
"@timestamp": "2018-11-22T06: 12: 00.307Z",
"host": "172.26.66.59",
"port": 44154,
"type": "nagios_core",
"epoch_timestamp": "1542867118",
"nagios_severity_label": "EXTERNAL COMMAND",
"nagios_external_command": "ACKNOWLEDGE_SVC_PROBLEM",
"nagios_host": "DE-Hoeheinoed-VOC1-SRV",
"nagios_service": "ntp_timesync",
"nagios_sticky": "2",
"nagios_notify": "0",
"nagios_persistent": "0",
"nagios_author": "Jaizel Jem Perdon",
"nagios_comment": "SN 307185410",
"utc_timestamp": "2018-11-22T06: 11: 58.000Z"
},
"sort": [
1542867120307
]
}
]
}
}
我在SQL中具有以下代码:但是,我的结果中得到空值。作为JSON的新功能,我无法找到JSON数据值的路径
Drop table if exists #Temp1
Declare @JSON nvarchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK '\\DKRDSDFSROOT10\Data\_Temp\MEIPE\ITE1452552_02test.json', SINGLE_CLOB) as j
select @json as details
If (ISJSON(@json) = 1)
BEGIN
PRINT 'JOSN File is valid';
select * into #Temp1
from OPENJSON(@JSON, '$.hits')
WITH
(
[nagios_author] nvarchar(100) '$.hits.hits._source.nagios_author',
[nagios_comment] nvarchar(100) '$.hits.hits._source.nagios_comment'
)
END
ELse
Begin
PRINT 'JOSN File is invalid';
END
select * from #Temp1
有人可以帮我吗?
答案 0 :(得分:0)
您将json中的第二个hits数组用作对象,但是它的数组尝试按以下方式更改查询:
select * into #Temp1
from OPENJSON(@JSON, '$.hits')
WITH
(
[nagios_author] nvarchar(100) '$.hits.hits[0]._source.nagios_author',
[nagios_comment] nvarchar(100) '$.hits.hits[0]._source.nagios_comment'
)
答案 1 :(得分:0)
我尝试使用下面的代码,并且有效
WITH
(
[nagios_author] nvarchar(100) '$.hits[0]._source.nagios_author',
[nagios_comment] nvarchar(100) '$.hits[0]._source.nagios_comment'
)