我在Sql Server中有以下JSon数据。
{
"PartnerName": null,
"PartnerCurrencyCode": "UAD",
"PricingConditions": [
{
"PricingConditionId": 659853,
"ConditionTypeCode": "ABCD",
"ConcessionItemTypeCode": "ABC",
"PriceLevel": null
}
],
"CurrencyMultiplier": 0
}
预期结果是:
PartnerName PartnerCode PartnerCurrencyCode PricingConditionId
NULL NULL UAD 659853
我已经读过Sql Server 2016具有读取JSON数据的功能,并尝试使用以下查询。
查询1:
SELECT * FROM OPENJSON(@json, '$')
查询2:
SELECT *
FROM OPENJSON(@json)
WITH (PartnerName NVARCHAR(50) '$.PartnerName',
PartnerCode nvarchar(50) '$.PartnerCode',
PartnerCurrencyCode nvarchar(50) '$.PartnerCurrencyCode',
PricingConditionId nvarchar(50) '$.PricingConditions.PricingConditionId')
查询2结果:
PartnerName PartnerCode PartnerCurrencyCode PricingConditionId
NULL NULL UAD NULL
我期待“PricingConditionId”的价值是 - 659853
答案 0 :(得分:3)
尝试此查询:
SELECT PartnerName,
PartnerCode,
PartnerCurrencyCode,
[PricingConditions[0]].PricingConditionId AS ProductID1
FROM OPENJSON (@json)
WITH (
PartnerName NVARCHAR(50),
PartnerCode NVARCHAR(50),
PartnerCurrencyCode NVARCHAR(50),
[PricingConditions[0]].PricingConditionId NVARCHAR(50)
) AS Partners
以下是link to a great article,其中讨论了处理问题的几种方法。