使用 OPENJSON 解析
Posted
技术标签:
【中文标题】使用 OPENJSON 解析【英文标题】:Parsing with OPENJSON 【发布时间】:2020-08-20 22:36:15 【问题描述】:我正在尝试从 GET 调用中获取响应并通过 OPENJSON
将其加载到 SQL Server 中,但我无法解析响应。
DECLARE @json NVARCHAR(MAX);
SET @json = N'
"LookupServiceType": "GetAssetValues",
"Items": [
"id": "19676",
"value": "\"AssetCode\":\"TDAACC\",\"Symbol\":null,\"Issue\":\"ACCOUNT #49\",\"Issuer\":\"TD AMERITRADE\""
,
"id": "19677",
"value": "\"AssetCode\":\"RE100\",\"Symbol\":null,\"Issue\":\"APN: 057\",\"Issuer\":\"SAN ANTONIO TX 78212\""
,
"id": "19908",
"value": "\"AssetCode\":\"NALIPO\",\"Symbol\":null,\"Issue\":\"POLICY # L0472\",\"Issuer\":\"NATIONWIDE LIFE\""
]
';
我尝试了很多变体,但我就是做不好。这是我尝试过的几件事。任何建议将不胜感激。
SELECT *
FROM OPENJSON(@json, '$.Items')
WITH (
Items int '$.id',
value NVARCHAR(MAX) '$.value',
AssetCode NVARCHAR(50) '$.value.AssetCode',
Symbol NVARCHAR(50) '$.value.Symbol',
Issue NVARCHAR(50) '$.value.Issue',
Issuer NVARCHAR(50) '$.value.Issuer'
);
SELECT id,AssetCode,Symbol,Issue, Issuer
FROM OPENJSON(@json)
WITH (
Items NVARCHAR(MAX) '$.Items' AS JSON
)
OUTER APPLY OPENJSON(Items)
WITH (
id INT 'strict $.id',
value NVARCHAR(MAX) '$.value' AS JSON
)
OUTER APPLY OPENJSON(value)
WITH (
AssetCode NVARCHAR(50) '$.AssetCode',
Symbol NVARCHAR(50) '$.Symbol',
Issue NVARCHAR(50) '$.Issue',
Issuer NVARCHAR(50) '$.Issuer'
);
【问题讨论】:
【参考方案1】:我想你正在寻找这个。 JSON 不需要任何更改
select j1.LookupServiceType, j2.id, j3.AssetCode, j3.Symbol, j3.Issue, j3.Issuer
from
OPENJSON(@json) WITH (LookupServiceType nvarchar(4000),
Items NVARCHAR(MAX) '$.Items' AS JSON) j1
outer APPLY
OPENJSON(Items) WITH (id INT 'strict $.id',
value NVARCHAR(MAX) '$.value' /*AS JSON*/) j2
outer apply
OPENJSON([value]) WITH (AssetCode NVARCHAR(50) '$.AssetCode',
Symbol NVARCHAR(50) '$.Symbol',
Issue NVARCHAR(50) '$.Issue',
Issuer NVARCHAR(50) '$.Issuer') j3;
结果
LookupServiceType id AssetCode Symbol Issue Issuer
GetAssetValues 19676 TDAACC NULL ACCOUNT #49 TD AMERITRADE
GetAssetValues 19677 RE100 NULL APN: 057 SAN ANTONIO TX 78212
GetAssetValues 19908 NALIPO NULL POLICY # L0472 NATIONWIDE LIFE
【讨论】:
【参考方案2】:问题是你的 JSON 字符串的格式,你有多余的转义和“”。
清理完 JSON 字符串后,您的第一个查询就可以正常工作了:
DECLARE @json NVARCHAR(MAX);
SET @json = N'
"LookupServiceType": "GetAssetValues",
"Items": [
"id": "19676",
"value": "AssetCode":"TDAACC","Symbol":null,"Issue":"ACCOUNT #49","Issuer":"TD AMERITRADE"
,
"id": "19677",
"value": "AssetCode":"RE100","Symbol":null,"Issue":"APN: 057","Issuer":"SAN ANTONIO TX 78212"
,
"id": "19908",
"value": "AssetCode":"NALIPO","Symbol":null,"Issue":"POLICY # L0472","Issuer":"NATIONWIDE LIFE"
]
';
SELECT *
FROM OPENJSON(@json, '$.Items')
WITH (
Items int '$.id',
AssetCode NVARCHAR(50) '$.value.AssetCode',
Symbol NVARCHAR(50) '$.value.Symbol',
Issue NVARCHAR(50) '$.value.Issue',
Issuer NVARCHAR(50) '$.value.Issuer'
);
结果:
Items AssetCode Symbol Issue Issuer
19676 TDAACC NULL ACCOUNT #49 TD AMERITRADE
19677 RE100 NULL APN: 057 SAN ANTONIO TX 78212
19908 NALIPO NULL POLICY # L0472 NATIONWIDE LIFE
或者,如果您不想修复 JSON,您可以使用以下语句:
SELECT *
FROM OPENJSON(REPLACE(REPLACE(Replace(@json, '\', ''), '"', ''), '"', ''), '$.Items')
WITH (
Items int '$.id',
value NVARCHAR(MAX) '$.value',
AssetCode NVARCHAR(50) '$.value.AssetCode',
Symbol NVARCHAR(50) '$.value.Symbol',
Issue NVARCHAR(50) '$.value.Issue',
Issuer NVARCHAR(50) '$.value.Issuer'
);
【讨论】:
以上是关于使用 OPENJSON 解析的主要内容,如果未能解决你的问题,请参考以下文章
使用 OPENJSON 跳过 OBJECTS 解析 JSON SQL