2017-12-28 2 views
0

JSON 데이터를 보유하는 열이 포함 된 SQL 테이블이 있습니다.값의 JSON 배열에서 특정 JSON 배열 검색

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' 

쿼리는 서면 같은를 반환

{ 
    "_id": "5a450f038104ca3cb0ff74b5", 
    "index": 3, 
    "guid": "20d807c5-bddc-44b9-97fe-fd18af1b6066", 
    "isActive": false, 
    "balance": "$2,832.38", 
    "picture": "http://placehold.it/32x32", 
    "age": 23, 
    "eyeColor": "brown", 
    "firstname": "Genevieve", 
    "lastname": "Green", 
    "gender": "female", 
    "company": "PROFLEX", 
    "email": "[email protected]", 
    "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" 
    } 

나는 다음과 같이 이름과 성 모든 친구를 검색하는 쿼리를 실행할 수 있습니다 : 다음과 같이 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" 
     ] 
    } 
] 

은 내가 실제로 원하는 것은 친구 이름의 단지 배열이 같은 것입니다 : FriendsList에 대한 JSON 문자열은 다음과 같습니다 그

["Mccoy Berg", "Chase Mcfadden", ...] 

이것이 가능하지만 JSON 지식은 제한적입니다.

+0

, '$ .friends :

는 먼저, 다음 수 STUFF 그래서 같은 FriendsList 필드에 그 이름을, 이름 만 반환하는 하위 쿼리를 작성합니다. 이름 ')'? – DavidG

+0

@DavidG - 시도해 보았습니다. NULL을 리턴합니다. –

+0

충분하겠습니까? '[name] NVARCHAR (25) '$ .name')와 함께'SELECT [Name] FROM OPENJSON (@json, '$ .friends')의 이름을 줄 것이다. – DavidG

답변

3

예상되는 형식으로 배열을 만들면 SQL 서버가 재미 있습니다. 기본적으로 JSON 배열은 항상 key : value 쌍으로 생성됩니다. JSON 작업시 STUFF()FOR XML을 사용하여이 문제를 해결할 수 있습니다.

당신이 JSON_QUERY (JsonValue`할 경우 어떻게됩니까
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> 
+1

또는 잠재적으로'FriendsList = '['+ (STRING_AGG (SELECT ' "'+ [이름] + '' ',' ') OPENJSON (JsonValue,'$. 친구)를 FROM \t \t \t \t \t ([이름] NVARCHAR (100) '$ .name')) + ']''OP가 사용중인 버전에 따라 다름 –