【发布时间】:2020-03-29 14:37:53
【问题描述】:
我有以下 JSON。我试图构建一个查询来获取名字、姓氏、iPhone 号码和家庭号码。我正在尝试使用 JSON 路径过滤器表达式。它不适合我。
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
使用的查询
DECLARE @jsonInfo NVARCHAR(MAX)
SET @jsonInfo=N'{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}'
SELECT
JSON_VALUE(@jsonInfo,'$.firstName') AS FirstName,
JSON_VALUE(@jsonInfo,'$.lastName') AS LastName
--JSON_VALUE(@jsonInfo,'$.phoneNumbers[?(@.type=="iPhone")].number') AS IPhoneNumber,
--JSON_VALUE(@jsonInfo,'$.phoneNumbers[?(@.type=="home")].number') AS HomeNumber
问候 阿米尔塔拉杰
【问题讨论】:
标签: json sql-server