从 JSON 值数组中检索特定的 JSON 数组
Retrieving a specific JSON array from a JSON array of values
我有一个 SQL table,其中包含一个包含 JSON 数据的列。 JSON 值之一如下所示:
{
"_id": "5a450f038104ca3cb0ff74b5",
"index": 3,
"guid": "20d807c5-bddc-44b9-97fe-fd18af1b6066",
"isActive": false,
"balance": ",832.38",
"picture": "http://placehold.it/32x32",
"age": 23,
"eyeColor": "brown",
"firstname": "Genevieve",
"lastname": "Green",
"gender": "female",
"company": "PROFLEX",
"email": "genevievegreen@proflex.com",
"phone": "+1 (919) 464-2866",
"address": "107 Clermont Avenue, Rew, California, 4298",
"about": "Magna pariatur ut enim nulla pariatur ad Lorem amet. Proident nulla exercitation id Lorem commodo minim cillum irure exercitation labore nostrud nostrud sint. Voluptate commodo ea commodo quis Lorem laborum culpa voluptate enim nulla enim duis.\r\n",
"registered": "2016-02-16T09:51:25 +05:00",
"latitude": -16.492643,
"longitude": -71.782118,
"tags": [
"in",
"non",
"eiusmod",
"labore",
"dolor",
"laboris",
"ullamco"
],
"friends": [
{
"id": 0,
"name": "Mccoy Berg",
"interests": [
"Music",
"Birding",
"Chess"
]
},
{
"id": 1,
"name": "Chase Mcfadden",
"interests": [
"Software",
"Chess",
"History"
]
},
{
"id": 2,
"name": "Michele Dodson",
"interests": [
"Football",
"Birding",
"Movies"
]
}
],
"greeting": "Hello, Genevieve! You have 2 unread messages.",
"favoriteFruit": "strawberry"
}
我可以执行一个查询来检索名字和姓氏以及所有朋友,如下所示:
SELECT
JSON_VALUE(JsonValue, '$.firstname') as FirstName,
JSON_VALUE(JsonValue, '$.lastname') as LastName,
JSON_QUERY(JsonValue, '$.friends') as FriendsList,
From <MyTable>
Where JSON_VALUE(JsonValue,'$.lastname') = 'Green'
查询,如所写,returns FriendsList 的 JSON 字符串,如下所示:
[
{
"id":0,
"name":"Mccoy Berg",
"interests":[
"Music",
"Birding",
"Chess"
]
},
{
"id":1,
"name":"Chase Mcfadden",
"interests":[
"Software",
"Chess",
"History"
]
},
{
"id":2,
"name":"Michele Dodson",
"interests":[
"Football",
"Birding",
"Movies"
]
}
]
我真正想要的只是一组朋友的名字,像这样:
["Mccoy Berg", "Chase Mcfadden", ...]
我确定这是可能的,但我对 JSON 的了解有限。
SQL 服务器以您期望的格式创建数组时有点有趣。默认情况下,它总是将 JSON 数组创建为 key:value 对。您可以在使用 JSON.
时使用 STUFF()
和 FOR XML
来解决此问题
您首先创建一个 returns 只有名称的子查询,然后您可以 STUFF
这些名称到 FriendsList
字段中,如下所示:
SELECT
FirstName = JSON_VALUE(JsonValue, '$.firstname')
,LastName = JSON_VALUE(JsonValue, '$.lastname')
,FriendsList = '[' + STUFF(
(SELECT ',' + '"' + [name] + '"'
FROM OPENJSON(JsonValue,'$.friends')
WITH ( [name] NVARCHAR(100) '$.name')
FOR XML PATH (''))
, 1, 1, '')
+ ']'
FROM <MyTable>
我有一个 SQL table,其中包含一个包含 JSON 数据的列。 JSON 值之一如下所示:
{
"_id": "5a450f038104ca3cb0ff74b5",
"index": 3,
"guid": "20d807c5-bddc-44b9-97fe-fd18af1b6066",
"isActive": false,
"balance": ",832.38",
"picture": "http://placehold.it/32x32",
"age": 23,
"eyeColor": "brown",
"firstname": "Genevieve",
"lastname": "Green",
"gender": "female",
"company": "PROFLEX",
"email": "genevievegreen@proflex.com",
"phone": "+1 (919) 464-2866",
"address": "107 Clermont Avenue, Rew, California, 4298",
"about": "Magna pariatur ut enim nulla pariatur ad Lorem amet. Proident nulla exercitation id Lorem commodo minim cillum irure exercitation labore nostrud nostrud sint. Voluptate commodo ea commodo quis Lorem laborum culpa voluptate enim nulla enim duis.\r\n",
"registered": "2016-02-16T09:51:25 +05:00",
"latitude": -16.492643,
"longitude": -71.782118,
"tags": [
"in",
"non",
"eiusmod",
"labore",
"dolor",
"laboris",
"ullamco"
],
"friends": [
{
"id": 0,
"name": "Mccoy Berg",
"interests": [
"Music",
"Birding",
"Chess"
]
},
{
"id": 1,
"name": "Chase Mcfadden",
"interests": [
"Software",
"Chess",
"History"
]
},
{
"id": 2,
"name": "Michele Dodson",
"interests": [
"Football",
"Birding",
"Movies"
]
}
],
"greeting": "Hello, Genevieve! You have 2 unread messages.",
"favoriteFruit": "strawberry"
}
我可以执行一个查询来检索名字和姓氏以及所有朋友,如下所示:
SELECT
JSON_VALUE(JsonValue, '$.firstname') as FirstName,
JSON_VALUE(JsonValue, '$.lastname') as LastName,
JSON_QUERY(JsonValue, '$.friends') as FriendsList,
From <MyTable>
Where JSON_VALUE(JsonValue,'$.lastname') = 'Green'
查询,如所写,returns FriendsList 的 JSON 字符串,如下所示:
[
{
"id":0,
"name":"Mccoy Berg",
"interests":[
"Music",
"Birding",
"Chess"
]
},
{
"id":1,
"name":"Chase Mcfadden",
"interests":[
"Software",
"Chess",
"History"
]
},
{
"id":2,
"name":"Michele Dodson",
"interests":[
"Football",
"Birding",
"Movies"
]
}
]
我真正想要的只是一组朋友的名字,像这样:
["Mccoy Berg", "Chase Mcfadden", ...]
我确定这是可能的,但我对 JSON 的了解有限。
SQL 服务器以您期望的格式创建数组时有点有趣。默认情况下,它总是将 JSON 数组创建为 key:value 对。您可以在使用 JSON.
时使用STUFF()
和 FOR XML
来解决此问题
您首先创建一个 returns 只有名称的子查询,然后您可以 STUFF
这些名称到 FriendsList
字段中,如下所示:
SELECT
FirstName = JSON_VALUE(JsonValue, '$.firstname')
,LastName = JSON_VALUE(JsonValue, '$.lastname')
,FriendsList = '[' + STUFF(
(SELECT ',' + '"' + [name] + '"'
FROM OPENJSON(JsonValue,'$.friends')
WITH ( [name] NVARCHAR(100) '$.name')
FOR XML PATH (''))
, 1, 1, '')
+ ']'
FROM <MyTable>